Star
Star

Reputation: 2299

Number of rows of each sub-cell in a cell array

I have a cell C in Matlab of dimension mx1, e.g. m=3

C={{1 2 3; 4 5 6} {4 5 6} {7 8 9 10; 11 12 13 14; 15 16 17 18}}

Is there a way to get the vector D of dimension mx1 reporting the number of rows of each sub-cell in C without using loops? In the example D=[2 1 3]'.

Upvotes: 1

Views: 102

Answers (2)

Dan
Dan

Reputation: 45752

This is identical to your last (now deleted) question, just use size instead of length:

D = cellfun(@(x)(size(x,1)), C)

But note that cellfun is just a wrapper for a for-loop so doing this does not avoid loops.

Note a better solution (from Luis Mendo's comment) is

[D, ~] = cellfun(@size, C)

This way you can get the number of rows and the number of columns in one shot:

[nr, nc] = cellfun(@size, c)

Upvotes: 4

user813853
user813853

Reputation:

Can you please try this two instructions :

cellsz = cellfun(@size,C,'uni',false);
cellsz{:}

you will get something like :

ans =
   4     2
ans =
  3    1
ans =
   5    3

Upvotes: 1

Related Questions