how to prevent overwriting in matrix while creating in for loop?

조회 수: 2 (최근 30일)
swarnali mukhopadhyay
swarnali mukhopadhyay 2016년 12월 11일
편집: Stephen23 2016년 12월 11일
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;
%middle nodes
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;
T(i,j)=0;
end
%end node
T(1,nx-1) = b;
T(1,nx) = b + c + a;
T(2,nx) = c;
C(nx) = a*To;
%sliding
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);
T(i-1,j) =1;
T (i,j-1) =1;
T(i,j) =-4;
T(i,j+1)= 1;
T(i+2,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
while creating the matrix the successive operation overwrites the values allocated previously.Is there anyway to void the overwriting?
  댓글 수: 1
Stephen23
Stephen23 2016년 12월 11일
편집: Stephen23 2016년 12월 11일
"Is there anyway to avoid the overwriting?"
Yes. Simply preallocate an output array of the correct size, and use indexing to assign the new values within the loop. You will find lots of examples online.

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

답변 (0개)

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by