Looping over column and returning values where conditions are met

조회 수: 9 (최근 30일)
Joseph McGorry
Joseph McGorry 2020년 4월 5일
편집: darova 2020년 4월 6일
I want to loop over a first column (which is column numbers) and return the values when the conditions are met for the next 4 columns. I already have a loop that finds the count of columns that meet conditions but I want it to return the first column values. For example in the image below the highlighted rows are the conditions that are satified and I want to find and reutrn the row numbers (3,5,6 and 9).
  댓글 수: 3
Joseph McGorry
Joseph McGorry 2020년 4월 5일
편집: darova 2020년 4월 6일
count=0;
n=length(matrix);
for k=1:n
if col2(k)>200 && col2(k)>300 && col3(k)>70
count=count+1;
end
end
this is what i have but only counts how many rows meet criteria it doesnt show which row numbers meet the criteria
Tommy
Tommy 2020년 4월 5일
편집: Tommy 2020년 4월 5일
If you want/need to keep the loop, you might do something like this:
count=0;
n=length(matrix);
idx=zeros(size(col1));
for k=1:n
if col2(k)>200 && col2(k)>300 && col3(k)>70
count=count+1;
idx(k)=col1(k);
end
end
idx(~idx)=[];

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

답변 (1개)

James Tursa
James Tursa 2020년 4월 5일
In general, perform find( ) on the condition you want. E.g.,
find(matrix(:,4)>80)
would return the row numbers where the 4th column is greater than 80.
  댓글 수: 4
Joseph McGorry
Joseph McGorry 2020년 4월 5일
Is it also possible to accomplish the same result by using a loop?
James Tursa
James Tursa 2020년 4월 5일
Sure. If your loop index k is looping over the rows, then you could test within the loop
if( matrix(k,4)>80 & matrix(k,3)<350 )
% do something
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by