find a vector in a cell array

19 visualizzazioni (ultimi 30 giorni)
Elysi Cochin
Elysi Cochin il 27 Apr 2019
Commentato: Walter Roberson il 27 Apr 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));

Risposta accettata

Walter Roberson
Walter Roberson il 27 Apr 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 Commento
Walter Roberson
Walter Roberson il 27 Apr 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');

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by