prevent overwriting in matrix

조회 수: 1 (최근 30일)
swarnali mukhopadhyay
swarnali mukhopadhyay 2016년 12월 11일
편집: Walter Roberson 2024년 10월 14일
T(1,1) = (a-b-c);
T(1,2)=b;
T(2,1) = c;
C(1) = a*To;
for j= 2:1:nx-1 i=1;
T(i,j-1) = b/2;
T(i,j) = 2*a-b-2*c;
T(i,j+1) = b/2;
T (i+1,j+1) = 2*c;
C(j) = 2*a*To;
end
T(1,nx-1) = b;
T(1,nx) = b + c + a;
T(2,nx) = c;
C(nx) = a*To;
for i = 2:1:nysl
for j= 2:1:nx
if(i<=nysl-1 && j==1);
T(i-1,j)=1;
T(i,j)= -4;
T(i+1,j)=1;
T(i,j+1)=2;
elseif (i<=nysl-1 && j<=nx-1);
if true
% code
end
T(i-1,j) =1;
T (i,j-1) =1;
T(i,j) =-4;
T(i,j+1)= 1;
T(i+1,j)=1;
elseif (i<=nysl-1 && j==nx);
T(i-1,j) =1;
T (i,j-1) =2;
T(i,j) =-4;
T(i+1,j)= 1;
else
T(i,j)=0;
end
end
end
due to the loop, the element gets overwritten by the next operation whereas i would want it to sum whichever the new element is to the previous value. kindly suggest someway to do so.

답변 (1개)

BhaTTa
BhaTTa 2024년 10월 14일
Hey @swarnali mukhopadhyay, you can overwrite previous state with sum of previous state and the new values inside the loop.
for example
T(i,j) = T(i,j)+new_value;

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by