Performing Iteration for a Matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
I want to perform an iterative operation on each element of a matrix according to this equation which is for a 3 x 3 matrix:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/906320/image.jpeg)
For example, by having this matrix:
a = [0 0.2 -0.6; -0.2 0 0.4; 0.6 -0.4 0]
I wrote the code for the first iteration for each element seperately but my original matrix is 1000 x 1000, so I need a code that can do this operation for the whole matrix. The code that I wrote for each element of the matrix is:
a_1(1,2) = a(1,2) + (0.25 * (a(1,3) + a(3,2))); %CF is 0.25
a_1(1,3) = a(1,3) + (0.25 * (a(1,2) + a(2,3)));
a_1(2,1) = a(2,1) + (0.25 * (a(2,3) + a(3,1)));
a_1(2,3) = a(2,3) + (0.25 * (a(2,1) + a(1,3)));
a_1(3,1) = a(3,1) + (0.25 * (a(3,2) + a(2,1)));
a_1(3,2) = a(3,2) + (0.25 * (a(3,1) + a(1,2)))
Which will give the result as shown above. The term that starts with CF on the RH is essentially saying no index is chosen twice in the same place. I'd appreciate it if someone can please help me. Thank you!
댓글 수: 0
채택된 답변
Torsten
2022년 2월 25일
편집: Torsten
2022년 2월 25일
L = 2;
CF = 0.25;
a = [0 0.2 -0.6; -0.2 0 0.4; 0.6 -0.4 0];
n = size(a,1);
A = zeros(L,n,n);
A(1,:,:) = a;
for l = 2:L
for i = 1:n
for j = 1:n
A(l,i,j) = A(l-1,i,j) - CF*(A(l-1,i,i) + 2*A(l-1,i,j) + A(l-1,j,j));
for k = 1:n
A(l,i,j) = A(l,i,j) + CF*(A(l-1,i,k) + A(l-1,k,j));
end
end
end
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!