Find the cell index in a nested cell array, corresponding to a string (by using strcmp)
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a 1×2 nested cell array A (cell array within a cell array):
A =
1×2 cell array
{43612×7 cell} {863892×7 cell}
where
A{1} =
{'Up' } {'Down' } {'Left' } {'Right'} {'BLUE'}
{'1' } {'7' } {'1' } {'8' } {'3' }
{'2' } {'7' } {'4' } {'3' } {'7' }
{'9' } {'1' } {'6' } {'2' } {'1' }
{'...' } {'...' } {'...' } {'...' } {'...' }
A{2} =
{'Up' } {'Down' } {'Left' } {'Right'} {'RED' }
{'2' } {'1' } {'1' } {'1' } {'2' }
{'6' } {'1' } {'7' } {'4' } {'2' }
{'5' } {'9' } {'8' } {'4' } {'4' }
{'...' } {'...' } {'...' } {'...' } {'...' }
If possible, by using only 1 or 2 lines of code, I would like to know if the text 'BLUE' is in A{1} or in A{2}.
The full answer would be: 'BLUE' is in A{1}(1,5), but I would like to know just the first index of A, i.e. either A{1} or A{2}.
Therefore, if we consider only the firs index of A, i.e. either A{1} or A{2}, my Result would be something like:
Result = 2x1 logical array
1
0
Which means my 'BLUE' text is in A{1}.
My first attempt started by using 'strcmp' in this way:
cellfun(@(x) strcmp(x, 'BLUE'), A, 'UniformOutput', false)
But then I got stuck... Any suggestion to get what I need in only 1 or 2 lines of code ?
Hope my question is clear enough.... Thanks a lot and best regards!
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (3)
Bruno Luong
am 16 Sep. 2019
Bearbeitet: Bruno Luong
am 16 Sep. 2019
A{1} = { ...
{'Up' } {'Down' } {'Left' } {'Right'} {'BLUE'}
{'1' } {'7' } {'1' } {'8' } {'3' }
{'2' } {'7' } {'4' } {'3' } {'7' }
{'9' } {'1' } {'6' } {'2' } {'1' }
{'...' } {'...' } {'...' } {'...' } {'...' }}
A{2} = {...
{'Up' } {'Down' } {'Left' } {'Right'} {'RED' }
{'2' } {'1' } {'1' } {'1' } {'2' }
{'6' } {'1' } {'7' } {'4' } {'2' }
{'5' } {'9' } {'8' } {'4' } {'4' }
{'...' } {'...' } {'...' } {'...' } {'...' }}
% testfun (ONELINE function)
% EDIT version
% returns 1 x 2 boolean arrays B, B(i) is TRUE if word exists in A{i}
testfun = @(word) ismember([0 1],find(strcmp([A{1}{:} A{2}{:}], word))>numel(A{1}));
testfun('BLUE')
testfun('RED')
testfun('Up')
testfun('YELLOW')
testfun('3')
testfun('5')
0 Kommentare
the cyclist
am 16 Sep. 2019
Bearbeitet: the cyclist
am 16 Sep. 2019
Here is a one-liner:
cellfun(@(y)any(any(cellfun(@(x)isequal(x,{'BLUE'}),y))),A)
I stumbled across this function for dealing with nested cell arrays. I did not try it myself, but it could make this a bit more elegant-looking.
0 Kommentare
Sim
am 16 Sep. 2019
5 Kommentare
Bruno Luong
am 16 Sep. 2019
Bearbeitet: Bruno Luong
am 16 Sep. 2019
Matt edit his code and correct the BUG, now his code runs about the same time as mine.
clear
createfun = @(s) arrayfun(@(x) {sprintf('%d',x)}, ceil(1000*rand(s)), 'unif',0);
A{1}=createfun([43612 7]);
A{2}=createfun([863892 7]);
% Matt J
tic
B = cellfun(@(x) strcmp([x{:}], '123'), A, 'UniformOutput', false);
matt_r = cellfun(@(c) any(c(:)), B);
toc % Elapsed time is 4.430430 seconds.
tic
testfun = @(word) ismember([0 1],find(strcmp([A{1}{:} A{2}{:}], word))>numel(A{1}));
bruno_r = testfun('123');
toc % Elapsed time is 3.967751 seconds.
% the cyclist
tic
cyclist_r = cellfun(@(y)any(any(cellfun(@(x)isequal(x,{'123'}),y))),A);
toc % Elapsed time is 22.860207 seconds.
Edit fix createfun BUG (forget inner {...})
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!