Count two columns with corresponding data
Show older comments
Hey guys! So im having a question regarding counting things in matlab. I have a Matrix with one column of direction and one column of the speed So what i want to do is to find all data that has lets say a direction between 0-45degrees and a speed of 0<x<3 And then i want to count how many there are. Why I cant do it by hand is because its a large sheet of data. I guess we want to use some if statements like if 0<direction<45 & 0<speed<3 . . . But I dont know how to write it. Hope u get what i mean, thanks!
Accepted Answer
More Answers (1)
One thing you can do is (assuming you have newer versions of matlab);
A = rand(10,2); % the data
a1 = A(A(:,1)<0.3,:); % find rows where 1st column is less than 0.3 in data
a2 = a1(a1(:,2)>0.9,:); % find rows where 2nd column is greater than 0.9 in a1
n = size(a2,1); % number of rows of data where 1st colum is less than 0.3 and 2nd
% column is greater than 0.9
If you have older version, you just need to use find function to create a1 and a2, i.e.;
index = find(A(:,1)<0.3&A(:,2)>0.9);
a = A(index,:);
n = length(index);
6 Comments
Guillaume
on 13 Sep 2018
I'm not aware that find predates logical indexing but if that's true, considering that R13SP2 from 2003, the earliest version whose documentation is still available online already supported logical indexing, you'd have to be on a very ancient version to be forced to use find.
Joakim Karlsson
on 13 Sep 2018
Aquatris
on 13 Sep 2018
yeah instead of A, use got29 variable. I just created A to give an example of the usage. You do not need to change anything else other than replacing A with your variable.
Joakim Karlsson
on 13 Sep 2018
Try the find method;
index = find( got29(:,5)>270& got29(:,5)<360);
a = got29(index,:); % rows got29 that satisfy the 270-360 in 5th column
n = length(index); % number of rows that satisfy the relation
Categories
Find more on Logical 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!