Info

此问题已关闭。 请重新打开它进行编辑或回答。

Need help creating a code

1 次查看(过去 30 天)
Sha S
Sha S 2015-8-19
关闭: MATLAB Answer Bot 2021-8-20
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
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
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’.

Andrei Bobrov
Andrei Bobrov 2015-8-19
At = A(:,1) <= -2;
B = sum(At);
C = sum(At & abs(A(:,2))>=2);

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by