compare length of arrays in a cell

3 次查看(过去 30 天)
good morning, I have acell array and i want to compare cell's length. Till now I used just t compare the equality of the cells using:
isequal(A{1,:})
A is the cell array.
I tried to run
isequal(length(A{1,:}))
but that's not correct.
What is the easiest way to achieve that, without using a or cycle???
Thanks
  6 个评论
ludvikjahn
ludvikjahn 2015-3-13
yes of course, I have just mistaken the brackets.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-3-13
编辑:Stephen23 2015-3-13
This is easy to do in one line using cellfun , diff and any:
>> A = {cell(1,3),cell(1,3),cell(1,3)};
>> B = {cell(1,5),cell(1,2),cell(1,9)};
>> any(~diff(cellfun(@numel,A)))
ans =
1
>> any(~diff(cellfun(@numel,B)))
ans =
0

更多回答(1 个)

per isakson
per isakson 2015-3-12
编辑:per isakson 2015-3-12
A hint based on some guessing
cac = {'abc','def', 'ghi'};
tmp = cellfun( @length, cac, 'uni', false );
isequal( tmp{:} )
returns
ans =
1
I failed to make a one-liner
&nbsp
Addendum
A variant more in line with the comments to the question
cac = {'abc','def', 'ghi'};
cac = { cac, cac, cac };
tmp = cellfun( @length, cac, 'uni', false );
isequal( tmp{:} )
returns
ans =
1
  2 个评论
ludvikjahn
ludvikjahn 2015-3-12
编辑:ludvikjahn 2015-3-12
sorry, what stands 'uni' for? just as an example of length?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by