Looping over column and returning values where conditions are met

9 views (last 30 days)
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 Comments
Joseph McGorry
Joseph McGorry on 5 Apr 2020
Edited: darova on 6 Apr 2020
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 on 5 Apr 2020
Edited: Tommy on 5 Apr 2020
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)=[];

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 5 Apr 2020
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 Comments
Joseph McGorry
Joseph McGorry on 5 Apr 2020
Is it also possible to accomplish the same result by using a loop?
James Tursa
James Tursa on 5 Apr 2020
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!