Count two columns with corresponding data
조회 수: 4 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
jonas
2018년 9월 13일
편집: jonas
2018년 9월 13일
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
Guillaume
2018년 9월 13일
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 <=.
추가 답변 (1개)
Aquatris
2018년 9월 13일
편집: Aquatris
2018년 9월 13일
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!