Duplicate a matrix using nested for loops
14 views (last 30 days)
In Matlab I have been trying to duplicate matrix A [2,4,1;6,7,2;3,5,9] to matrix B using nested for loops. I know the general format uses an algorithm within the loop ie.
B(m,n)= *algorithm here*
I can not find an algorithm that would work for this matrix and I believe that it was designed this way on purpose. I have tried pulling numbers out of matrix A using
but this just assigns the values for the last column of A to a 1x3 matrix B (B=[1;2;9]
I have also tried scouring the www for help with how to duplicated a matrix that is not algorithmic using nested for loops to no avail.
Any help or guidance would be much appreciated.
Roger Stafford on 20 Feb 2018
What's wrong with
B = A; % ?
However, if you absolutely must do it with nested for-loops then do:
[m,n] = size(A);
B = zeros(m,n);
for i1 = 1:m
for i2 = 1:n
B(i1,i2) = A(i1,i2);