Reshaping (M,N)-matrix to (M,1)-matrix
Show older comments
Hello everyone,
I have a matrix, for example:
A = [1, 2, 3;
4, 5, 6;
7, 8, 9]
and now I want to create a matrix:
A = [123;
456;
789]
Does anybody know how I can do this efficiently?
(I need to do this 62.000 times for my matrix)
Accepted Answer
More Answers (2)
Jos (10584)
on 8 Nov 2013
Another trick using strings:
A = [1 2 3 ; 10 11 12 ; 90 0 99]
B = str2num(sprintf([repmat('%d',1,size(A,2)) ' '],A.')).'
2 Comments
Andrei Bobrov
on 8 Nov 2013
+1
Jos (10584)
on 8 Nov 2013
Note that you can play around with the "%d". For instance, you can use "%02d" so that a single digit number will be have a leading zero.
Hi!
So your resulting matrix is a vector. right?
Try this
mat10 = ones(size(A, 1), 1) * 10.^((size(A, 2)-1):-1:0);
sum(A .* mat10, 2)
This is applicable for matrices of any size.
In string form
str2num(char(regexprep(cellstr(num2str(A)), ' ', '')))
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!