find a vector in a cell array

조회 수: 16 (최근 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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by