필터 지우기
필터 지우기

compare length of arrays in a cell

조회 수: 3 (최근 30일)
ludvikjahn
ludvikjahn 2015년 3월 12일
댓글: Adam 2015년 3월 13일
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.
Adam
Adam 2015년 3월 13일

댓글을 달려면 로그인하십시오.

채택된 답변

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?
per isakson
per isakson 2015년 3월 12일
편집: per isakson 2015년 3월 12일
It's short for 'UniformOutput'. See cellfun, Apply function to each cell in cell array

댓글을 달려면 로그인하십시오.

카테고리

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