Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Need help creating a code
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I have a 8x2 matrix.
A= [-1 -1; 2 -2; 0 -1; -2 -2; -3 1;0 2; -3 4; -2 0]
I want to do two things:
1. I want to see how many values there are in total in the first column which are either equal to -2 or less than -2.
2. Of those values in column 1 that are either equal to -2 or less than -2, I want to know if the corresponding value in the 2nd column is either greater than or equal to 2 OR less than or equal to -2.
For the above example in the end I would want...
B= 4 (because there are 4 values in column 1 that are less than or equal to -2).
C= 2 (because of the 4 values in column 1 that are equal to or less than -2, only 2 of their corresponding numbers in column 2 are either greater than/equal to 2 OR less than/equal to -2.
1 件のコメント
dpb
2015 年 8 月 19 日
This seems straightforward enough to request like in homework, "show your work" -- what have you done so far and where did you get stuck, precisely?
回答 (2 件)
Star Strider
2015 年 8 月 19 日
編集済み: Star Strider
2015 年 8 月 19 日
Not sure how you want them presented:
A= [-1 -1; 2 -2; 0 -1; -2 -2; -3 1;0 2; -3 4; -2 0];
A1le2 = A(:,1) <= -2; % A(:,1) <= -2
B = sum(A1le2); % Number Of A(:,1) <= 2
A2ge2 = A(A1le2,2) >= 2; % A(:,1) <= -2 & A(:,2) >= +2
A2le2 = A(A1le2,2) <= -2; % A(:,1) <= -2 & A(:,2) <= -2
C = sum([A2ge2; A2le2]); % Number Of A(:,1) <= 2 & A(:,2) >= +2 & A(:,2) <= -2
EDIT — Specified ‘B’, added ‘C’.
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!