I have a large matrix (1st image). I need to update element in column 3&4 whenver it becomes 0 in column 5 or integer changes in column 6.
And I want to update it with sum from column 1 and 2.
See second image for more clarity.
My matrix should perform this for all the rows.

댓글 수: 1

Ravi Kumar
Ravi Kumar 2021년 10월 26일
Tried this, but not working. (still getting same values in column 3&4)
N = size(P,1);
for i = 1:1:N
if P(N,5) == 0
P(N,3) = P(N,1) + P(N,3);
end
end

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

 채택된 답변

Cris LaPierre
Cris LaPierre 2021년 10월 26일

1 개 추천

Use logical arrays (Ch 12 of MATLAB Onramp).
Perhaps something like this (untested)
P(P(:,5)==0,3)=sum(P(P(:,5)==0,[1,3],2));
P(P(:,5)==0,4)=sum(P(P(:,5)==0,[2,4],2))

추가 답변 (1개)

Rik
Rik 2021년 10월 26일

1 개 추천

N = size(P,1);
for k = 1:1:N
if P(k,5) == 0
P(k,3) = P(k,1) + P(k,3);
end
end
Similar code will allow you to edit when the value changes:
if k>1 && A(k-1)==A(k)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2021년 10월 26일

답변:

2021년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by