I am trying to write a simple code to replace one specific value in a matrix by a range of numbers. To be clearer, assuming I have my matrix G, which is a 3x3 matrix, I want to replace the (3,3) value in G with (3,3)+i where i is 1 - 10. I want to create essentially 10 new matrices where (3,3) is replaced by (3,3)+1 and then (3,3)+2 and then (3,3)+3 up to a value of 10. The original matrix is:
G=[2 4 5; 6 7 8; 8 8 2]
So, the output should a series of 10 new G matrices, so (3,3)+1 should give me
G=[2 4 5; 6 7 8; 8 8 3]
and for (3,3)+2 it should give me:
G=[2 4 5; 6 7 8; 8 8 4]
I know I can recode each matrix line by line but I want to create a loop to do this efficiently and to possibly change i to .001 increments rather than steps of 1.
I tried to do a simple code but I know I am missing the output part and I am having some bracket issues.
for i = 1:10;
B[3,3]= B[3,3]+i;
end
It is quite simple:
1) Make a cell array: B = cell(1,10);
2) Make a matrix with 1 on position (3,3) and zeros elsewhere: M33 = zeros(3,3); M33(3,3) = 1;
M33 = zeros(3,3); M33(3,3) = 1;
3) Loop through the numbers from 1 to 10 and save G+i*M33
to the i
th entry of B
:
for i = 1:10
B{i} = G+i*M33;
end
You can now access the i
th matrix as B{i}
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.