Filter löschen
Filter löschen

compare length of arrays in a cell

3 Ansichten (letzte 30 Tage)
ludvikjahn
ludvikjahn am 12 Mär. 2015
Kommentiert: Adam am 13 Mär. 2015
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 Kommentare
ludvikjahn
ludvikjahn am 13 Mär. 2015
yes of course, I have just mistaken the brackets.
Adam
Adam am 13 Mär. 2015

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 13 Mär. 2015
Bearbeitet: Stephen23 am 13 Mär. 2015
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

Weitere Antworten (1)

per isakson
per isakson am 12 Mär. 2015
Bearbeitet: per isakson am 12 Mär. 2015
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 Kommentare
ludvikjahn
ludvikjahn am 12 Mär. 2015
Bearbeitet: ludvikjahn am 12 Mär. 2015
sorry, what stands 'uni' for? just as an example of length?
per isakson
per isakson am 12 Mär. 2015
Bearbeitet: per isakson am 12 Mär. 2015
It's short for 'UniformOutput'. See cellfun, Apply function to each cell in cell array

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by