Length of cells within cell array

41 views (last 30 days)
Tha saliem
Tha saliem on 6 Apr 2017
Commented: zahra farhadpour on 30 Jul 2020
Hey i have a question that if i have a cell array containing multiple elements and each element in cell array have more elements like this:
x{1,1}{1,1}=1 x{1,1}{2,1}=[2;3;4]
x{2,1}{1,1}=[3;1] x{2,1}{2,1} x{2,1}{3,1}=[1;5;2;4] and all others elements like this.
My question is that how can I get length of each cell. Like for x{1,1}{1,1}, result is 1. For x{2,1}{2,1} x{2,1}{3,1}, result is 4
Thanks in advance.

Accepted Answer

Jan
Jan on 6 Apr 2017
Edited: Jan on 6 Apr 2017
x{1,1}{1,1} = 1;
x{1,1}{2,1} = [2;3;4];
x{2,1}{1,1} = [3;1];
x{2,1}{3,1} = [1;5;2;4];
The trivial answer:
length(x{1,1}{1,1})
length(x{2,1}{3,1})
To get all lengths at once:
Len = cellfun(@(c) cellfun('length', c), x, 'UniformOutput', false)
Now Len{2}(3) is 4.
  2 Comments
Tha saliem
Tha saliem on 6 Apr 2017
Edited: Tha saliem on 6 Apr 2017
yes i wanted to get all lengths. Thank you so much for help.
zahra farhadpour
zahra farhadpour on 30 Jul 2020
HI look at here for example i have a cell v=3*1
the cell array is v=[{1,2,3},{4,5},{7,7,,7,7}]
if you typesize( v(3) )you see 1
if yo want size v(3)as 4 you should typesize( v{3})

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!