find a vector in a cell array

18 visualizaciones (últimos 30 días)
Elysi Cochin
Elysi Cochin el 27 de Abr. de 2019
Comentada: Walter Roberson el 27 de Abr. de 2019
AP = {[1,2,14];[1,5,14]}
P = {[1,5,14]};
i wanted to check if P is in AP
i did as
IsInAp = find(cellfun(@(x) ismember(path,x,'rows'),allpaths));
but showing error as
Warning: The 'rows' input is not supported for cell array inputs.
> In cellismemberlegacy (line 47)
In cell/ismember (line 91)
In Untitled>@(x)ismember(path,x,'rows')
In Untitled (line 40)
Error using cell/ismember (line 34)
Input A of class cell and input B of class double must be cell arrays of character vectors,
unless one is a character vector.
Error in cellismemberlegacy (line 53)
[lia,locb] = ismember(a,b);
Error in cell/ismember (line 91)
lia = cellismemberlegacy(a,b,flag1);
Error in Untitled>@(x)ismember(path,x,'rows')
Error in Untitled (line 40)
IsInAp = find(cellfun(@(x) ismember(P,x,'rows'),AP));

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Abr. de 2019
any(cellfun(@isequal,AP(:),repmat(P,length(AP),1)))
This does not require that the elements of AP be the same length or that P and AP contain row vectors (but does require they be the same orientation.)
You could also use
ismember(P{1}, cell2mat(AP(:)), 'rows')
which does assume that elements of AP are the same length and that P and AP contain row vectors.
  1 comentario
Walter Roberson
Walter Roberson el 27 de Abr. de 2019
If you need to know the position then in the first version you can change the any() to find()
In the second version, you can add a second output,
[IsInAp, idx] = ismember(P{1}, cell2mat(AP(:)), 'rows');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by