Transform rows to columns and add them at the end

2 views (last 30 days)
Hello
I have a matrix like this:
1 2 3
4 5 6
7 8 9
Now, I want to transform it to this:
1
2
3
4
5
6
7
8
9
Note, the real values are not in row, so sort is no option.
Thanks in advance.

Accepted Answer

Mathieu NOE
Mathieu NOE on 14 Apr 2022
hi
simply this :
>> a = [1 2 3 ;
4 5 6;
7 8 9];
>> a(:)
ans =
1
4
7
2
5
8
3
6
9
  2 Comments
Mathieu NOE
Mathieu NOE on 14 Apr 2022
oops
this is better :)
a = [1 2 3 ;
4 5 6;
7 8 9];
b = a';
b(:)
ans =
1
2
3
4
5
6
7
8
9

Sign in to comment.

More Answers (1)

Alexander Bächi
Alexander Bächi on 14 Apr 2022
Edited: Alexander Bächi on 14 Apr 2022
I found a way. Maybe not beautiful but functional:
a =
1 2 3
4 5 6
7 8 9
iii = 1;
for i = 1:rows(a);
for ii = 1:columns(a);
b(iii,1) = a(i,ii);
iii++;
end;
end;
b =
1
2
3
4
5
6
7
8
9

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!