find a vector in a cell array

21 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by