Info

This question is closed. Reopen it to edit or answer.

Need help creating a code

1 view (last 30 days)
Sha S
Sha S on 19 Aug 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
dpb
dpb on 19 Aug 2015
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?

Answers (2)

Star Strider
Star Strider on 19 Aug 2015
Edited: Star Strider on 19 Aug 2015
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 on 19 Aug 2015
At = A(:,1) <= -2;
B = sum(At);
C = sum(At & abs(A(:,2))>=2);

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!