# Add a constant to a selected diagonal elements after every iteration.

4 views (last 30 days)
Kinga Gyeltshen on 13 Jul 2021
Commented: Kinga Gyeltshen on 13 Jul 2021
Hi everyone, I have a 9x9 matrix to which i want to add a constant of say 100i starting from the 4th element of the diagonal to the 9th. I would like to get an output after every operation/iteration (i.e. after adding 100i on each diagonal) but the value of 100i should be removed when the next operation is performed. Can anyone suggest me a method to achieve this objective? Thank you in advance.

Devanshu Shah on 13 Jul 2021
Hi Kinga!
% Let us call the matrix A
for i = 1:num_iterations
%Do some operations
A(4, 4)=A(4, 4)+ 100i;
A(5, 5)=A(5, 5)+ 100i;
A(6, 6)=A(6, 6)+ 100i;
A(7, 7)=A(7, 7)+ 100i;
A(8, 8)=A(8, 8)+ 100i;
A(9, 9)=A(9, 9)+ 100i;
% print the matrix
disp(A);
% reset the addition of 100i by subtracting it
A(4, 4)=A(4, 4)- 100i;
A(5, 5)=A(5, 5)- 100i;
A(6, 6)=A(6, 6)- 100i;
A(7, 7)=A(7, 7)- 100i;
A(8, 8)=A(8, 8)- 100i;
A(9, 9)=A(9, 9)- 100i;
end
##### 1 CommentShowHide None
Kinga Gyeltshen on 13 Jul 2021
Hi @Devanshu Shah it's not just the addition and subtraction of the value of 100i to the matrix. May be I did not make myself clear while putting up the question. Let me elaborate it here, I already have a 9x9 matrix and starting from the 4th element of the diagonal, I want to add 100i and obtain the reduced 3x3 matrix. Say when I add 100i to the 4th element I get a reduced matrix with the implication due to the 100i and during the next iteration I add the 100i to the 5th element but not on 4th this time and the process repeats till the 9th and by doing so I obtain 7 different 3x3 reduced matrix.