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!!!

 채택된 답변

Image Analyst
Image Analyst 2020년 12월 20일

0 개 추천

Try this:
rowsToSum = A(:, 2) < 0;
theSum = sum(A(rowsToSum, 1)); % Sum first column where second column is negative.

댓글 수: 1

anil hamzacebi
anil hamzacebi 2020년 12월 20일
It works exactly the way i need. Thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

VBBV
VBBV 2020년 12월 20일
편집: VBBV 2020년 12월 20일

1 개 추천

%true
Total=sum(A(A(:,2)<0))

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 12월 20일

댓글:

2020년 12월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by