Indexing way to look for specifc cell arrays in a cell array of cell arrays?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi! I have a cell array of "ordered-pair" cell arrays of strings, e.g.,
A = { {'aa','bb'}, {'cc','dd'}, {'ee','ff'} };
I'd like an indexing (i.e., not-explicitly-looping) way to return the "outer-most" indices of a single matching ordered-pair, e.g., if
X = {'cc', 'dd'};
then
whereEqual(X,A)
should return
[0 1 0].
Similarly, is there a not-explicitly-looping way to "flatten" A:
flatten(A) => {'aa', 'bb', 'cc', 'dd', 'ee', 'ff'}
Thanks for your help!
6 comentarios
Jan
el 25 de Ag. de 2011
The two problems already included in the wish-list: 1. The message disappears under some strange circumstances (something with empty lines, editing etc). 2. No notification for new comments.
Respuestas (3)
Jan
el 25 de Ag. de 2011
A = { {'aa', 'bb'}, {'xx', 'yy'} };
B = { {'aa', 'bb'}, {'cc', 'dd'} };
function R = isMemberC(A, B)
R = false(size(A));
for i = 1:numel(A)
for j = 1:numel(B)
if isequal(A{i}, B{j})
R(i) = true;
break;
end
end
end
And according to your other thread: If a case-insensitive comparison is wanted, change the "if isequal..." line to:
function R = isMemberC(A, B)
R = false(size(A));
for i = 1:numel(A)
sizeA = size(A);
for j = 1:numel(B)
if isequal(sizeA, size(B{j}) && all(strcmpi(A{i}, B[j}))
R(i) = true;
break;
end
end
end
4 comentarios
Fangjun Jiang
el 25 de Ag. de 2011
Yes. Why so obsessed with no-loopers? My hair straights up whenever I saw cellfun with anonymous function.
Jan
el 25 de Ag. de 2011
@David: Sometimes the most primitive implementation needs the fewest resources. An important rule is coming from the field of house economics: Never touch an item twice. In the original field, this means, that I put the dishes in the right place, if I hold them in my hands. For the programming it means, that it is efficient to reduce the number of accesses to the data. ISMEMBER involves two sortings and sorting means excessive data access. CELLFUN is a C-Mex function, which has to call MATLAB to perform the evaluation of the function handle for each cell element. And the needed mexCellMATLAB function has a lot of overhead. Therefore the CELLFUN(@isempty) is so much slower than CELLFUN('isempty'), which checks the dimensions inside the Mex function.
I'm using a lot of faster replacements for built-in MATLAB functions. You can look in my FEX submissions for some examples.
Fangjun Jiang
el 25 de Ag. de 2011
You probably need to do it using for-loops.
A = { {'aa', 'bb'}, {'xx', 'yy'} };
B = { {'aa', 'bb'}, {'cc', 'dd'} };
Index=false(size(A));
for k=1:length(A)
for j=1:length(B)
if isequal(A{k},B{j})
Index(k)=true;
break;
end
end
end
2 comentarios
Andrei Bobrov
el 25 de Ag. de 2011
R = all(reshape(ismember([A{:}],[B{:}]),[],numel(A)))
OR
R = all(reshape(strcmp([A{:}],[B{:}]),[],numel(A)))
ADD
c = mat2cell(cellfun(@(x)[x{:}],[B';A'],'un',0),[numel(B);numel(A)],1)
R = ismember(c{:})';
4 comentarios
Andrei Bobrov
el 26 de Ag. de 2011
mat2cell(cellfun(@(x)[x{:}],[B';A'],'un',0),[numel(B);numel(A)],1)
ismember(ans{:})
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!