Search through every vector in cell array for the vector that contains a certain value
이전 댓글 표시
Hiya,
I have the below code, which instead of searching through every vector in the cell array for a vector that contains the number 1, it goes to the last vector in the cell array and returns false.
my cell array is called nodey and contains in position {1,1} - [1,2] and in position {1,2} - [3,4]
for i = 1:length(nodey)
ismember(nodey{i},1);
end
I need it so that it returns true for the first vector, and then i can return nodey{i} and it'll be [1,2] since the value is in the first vector
Thanks!
채택된 답변
추가 답변 (1개)
dpb
2020년 4월 12일
Your code above doesn't save the result going thru the loop so you only show the final result at the end---
ix=cellfun(@(v) ismember(1,v),nodey,'UniformOutput',1);
>> ix
ix =
2×1 logical array
1
0
>>
Better form for coding would be to put the value to search for in a variable so can only change the variable to look for alternate values...
VFIND=1;
ix=cellfun(@(v) ismember(VFIND,v),nodey,'UniformOutput',1);
BTW: NB the order of arguments to ismember to have the first value the one-element searched-for value so returns a single result instead of a logical vector of numel(nodey) as the first output.
카테고리
도움말 센터 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!