how to sum specific matrix elements?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
anil hamzacebi
am 20 Dez. 2020
Kommentiert: anil hamzacebi
am 20 Dez. 2020
Hello everyone,
i have a 10x2 matrix. i want to sum all the elements in the first column while the second column has the negative values. for the example matrix below,
i want a b matrix that gives me 2+4+4 result. (2,4 and 4 are the first column numbers which has negative numbers in the second column)
A=[2 -3;4 -4;5 3;4 -2;5 4;1 3];
I hope you can help me, thank you all!!!
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 20 Dez. 2020
Try this:
rowsToSum = A(:, 2) < 0;
theSum = sum(A(rowsToSum, 1)); % Sum first column where second column is negative.
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!