Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Need help creating a code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
dpb
am 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?
Antworten (2)
Star Strider
am 19 Aug. 2015
Bearbeitet: Star Strider
am 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’.
0 Kommentare
Andrei Bobrov
am 19 Aug. 2015
At = A(:,1) <= -2;
B = sum(At);
C = sum(At & abs(A(:,2))>=2);
0 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!