MATLAB Answers

0

Generating a vector containing the sizes of all cells of a cell array, along a specific dimension

Asked by Paolo Binetti on 25 Jan 2017
Latest activity Commented on by Paolo Binetti on 26 Jan 2017
Is there a way to vectorize this? (I tried with "cellfun", but did not succeed).
for i = 1:numel(a)
s(i) = size(a{i}, 1);
end
Where a sample of "a" is provided in attachment.

  2 Comments

It would be much easier to help if you posted code that actually creates a small example of a, rather than pseudocode that illustrates what you mean.
Is
AGA
AGA
meant to represent a 2x3 character array? So, is it correct that
a{1,1} = ['AGA';'AGA'];
would be an example of one element? It seems like that's what you mean, but wanted to be sure.

Sign in to comment.

2 Answers

Answer by the cyclist
on 25 Jan 2017
Edited by the cyclist
on 25 Jan 2017
 Accepted Answer

Does this give what you want?
s = cellfun(@(x)size(x,1),a);

  1 Comment

Sign in to comment.


Answer by Jan
on 26 Jan 2017
Edited by Jan
on 26 Jan 2017

cellfun has some builtin-functions, which are provided as strings and much faster than using anomyous functions:
s = cellfun('size', a, 1)
Unfortunately they appear in the docs as "Backward compatibility" only, but they are jewels.

  1 Comment

Sign in to comment.