MATLAB Answers

How to gracefully generate an all-one cell array?

1 view (last 30 days)
An all-one array can be gracefully generated by ones(...)
But what if I want an all-one cell array?
function Output=OnesInCell(varargin)
%This function should be the same as ones() except that each of the ones is packed into an individual cell.
end
For-loops are not graceful!

  2 Comments

Rik
Rik on 29 Oct 2019
@Alex, it looks to me like this is the desired result, so I would suggest moving your comment to the answer section.

Sign in to comment.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 29 Oct 2019
Edited: Alex Mcaulley on 29 Oct 2019
function Output=OnesInCell(varargin)
%This function should be the same as ones() except that each of the ones is packed into an individual cell.
Output = num2cell(ones(varargin));
end

  2 Comments

Rik
Rik on 29 Oct 2019
Note that to actually use this function, you should use varargin{:} in your call to ones.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019b