필터 지우기
필터 지우기

Find rows based on set of values/codes

조회 수: 3 (최근 30일)
Millie Johnston
Millie Johnston 2022년 9월 13일
댓글: Star Strider 2022년 9월 14일
Hello community,
I have a data set that is 48x14 (called data for this example). Each row has a set of codes (possible codes are 0 1 13 14 15 16 31 42 53 54). a row might look like this: [1 0 0 13 0 0 31 0 0 0 42 0 0 53].
The code is part of a for loop but I am strugglig with one specific part: I want to find rows that have a specific series of codes while ignoing the 0 codes. I thought the following code would work to find rows with the codes 1 13 31 42 and 53 in it:
for i = 1:length(data(:,1))
if all(data(i,find(data(i,:) == [1 13 31 42 53])))
.......
.......
I get the following error:
Arrays have incompatible sizes for this operation.
Any help would be much appreciated!

채택된 답변

Star Strider
Star Strider 2022년 9월 13일
I am not certain what you want, however the ismember function might be a better option than find for this, especially since everything seems to be integers, so no floating-point approximation problems will be present (requiring ismembertol and likely more coding) —
data = [1 0 0 13 0 0 31 0 0 0 42 0 0 53];
q = ismember(data, [1 13 31 42 53])
q = 1×14 logical array
1 0 0 1 0 0 1 0 0 0 1 0 0 1
v = data(q)
v = 1×5
1 13 31 42 53
Then determine what you want to do with either ‘q’ (using the nnz function could give the number of matches) or ‘v’ (the content of the matches in the vector) here.
.
  댓글 수: 4
Millie Johnston
Millie Johnston 2022년 9월 14일
Excellent! Thank you very much!
Star Strider
Star Strider 2022년 9월 14일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Hill
David Hill 2022년 9월 13일
for i = 1:size(data,2)
if all(ismember(data(i,:),[0 1 13 31 42 53]))%include 0
  댓글 수: 2
Image Analyst
Image Analyst 2022년 9월 13일
This answer doesn't care about the order of the desired sequence of codes you are looking for or if there are any duplicated numbers in the row.
My answer below requires that the sequence match exactly (not be scrambled or in arbitrary order like [13, 53, 42, 31, 1]).
We're not sure which way you want it, but now you have it both ways so you can choose.
Millie Johnston
Millie Johnston 2022년 9월 13일
I am not looking for the sequnce per say, I am looking for the rows in which those numbers appear together (in any order). Does that make more sense?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 9월 13일
Try this
oneRow = [1 0 0 13 0 0 31 0 0 0 42 0 0 53];
oneRow(oneRow == 0) = [] % Remove zeros
oneRow = 1×5
1 13 31 42 53
In addition you should be using isequal
% Define the specific sequence you are looking for.
desiredCodeSequence = [1 13 31 42 53]; % Row should be exactly this.
% See if it matches your zero-removed row.
if isequal(oneRow, desiredCodeSequence)
% Matched
fprintf('Matched.\n')
else
% The row is not the desired sequence.
fprintf('The row is not the desired sequence.\n')
end
Matched.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by