Count two columns with corresponding data

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

jonas
jonas on 13 Sep 2018
Edited: jonas on 13 Sep 2018
Try this:
x is speed, y is direction
sum(x>0 & x<3 & y>0 & y<45)
the conditions inside of the braces gives a logical array which yields true (1) when satisfied and otherwise false (0).
EDIT: removed brackets

6 Comments

The square brackets are unnecessary and personally, I find them confusing, possibly indicating a bug that the author meant to concatenate the expression with something else and then forgot.
sum(x>0 & x<3 & y>0 & y<45)
or
nnz(x>0 & x<3 & y>0 & y<45)
jonas
jonas on 13 Sep 2018
Edited: jonas on 13 Sep 2018
Yes, my original answer did not have the square brackets. Added them later alongside my brief explanation. Was hoping it'd make the code easier to understand but I suppose it had the opposite effect :)
I've removed them again!
That works somewhat, but the hard thing is that we want to find e.g all speeds that are between 315 degrees and 45 degrees, but the top end if you know what I Mean. We would say -45 to 45 degrees, but the data is only in 0 to 360 :/ :/
sum(x>0 & x<3 & ((y>315 & y<360) | (y>0 & y>45)))
there you go. An alternative would be this solution:
sum(x>0 & x<3 & cosd(y)>cosd(45))
We would say -45 to 45 degrees, but the data is only in 0 to 360
It's a simple matter of shifting to [-180:180]
direction = mod(direction + 180, 360) - 180; %shift 0:360 to -180:180 range
count = nnz(speed > 0 & speed < 3 & direction > -45 & direction < 45)
Or you keep your 0:360 range and adapt your comparison (which needs splitting in two ranges, 0-45 and 315-360)
count = nnz(speed > 0 & speed < 3 & ((direction > 0 & direction < 45) | (direction > 315 & direction < 360)))
Note that you may want to change some of these > and < into >= and <=.
Thanks guys I will probably solve it now with your help!

Sign in to comment.

More Answers (1)

Aquatris
Aquatris on 13 Sep 2018
Edited: Aquatris on 13 Sep 2018
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

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.
Hmm okay. But my data is in my workspace called "got29" and thats where the columns are too. So I need to take them from there....
Aquatris
Aquatris on 13 Sep 2018
Edited: Aquatris on 13 Sep 2018
I did have troubles using logical indexing with 2013a. It might have been my inexperience at the time.
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.
It somewhat works but I can't get the "&" to work. So I want to do something like. a1 = got29(got29(:,5))>270 & got29(got29(:,5)<360,:);
So I find all rows that has between 270 and 360. But I gett error "Subscribt indices must either be real positive integers"
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

Sign in to comment.

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!