MATLAB Answers

0

how to convert a 3*3 matrix into 1*9 using for loop

Asked by Gourav Tanwar on 18 Aug 2019
Latest activity Commented on by darova
on 18 Aug 2019 at 14:26
i want to convert a 3*3 into 1*9 matrix using for loop
can anyone help

  0 Comments

Sign in to comment.

2 Answers

Answer by madhan ravi
on 18 Aug 2019

reshape(matrix,1,[]) % no loops needed , the beauty of MATLAB is the tasks are far simpler than most think.

  0 Comments

Sign in to comment.


Answer by Rob Weh
on 18 Aug 2019 at 13:18

Use reshape to do this. However, if you really want it in a loop for some reason use:
A=magic(3);
B=zeros(numel(A),1);
for ii=1:numel(A)
B(ii)=A(ii);
end

  1 Comment

darova
on 18 Aug 2019 at 14:26
Remember also (no matter: reshape or loop):
A = [1 2 3
4 5 6
7 8 9];
B = reshape(A,1,[]); % or A(:) or "for" loop
>> B
1 4 7 8 5 2 9 6 3

Sign in to comment.