find a vector in a cell array

17 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2019 年 4 月 27 日
コメント済み: Walter Roberson 2019 年 4 月 27 日
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));

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 27 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 27 日
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');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by