Reshape a 'column' matrix into a 'row' matrix
1 view (last 30 days)
Show older comments
Hi, I hope you are well.
I have a matrix 'X' that is generated through a series of parameters, that in the end of the day is a column-wise concatenated matrix such as:
X = [A; B; C; ...; P], where A, B, C, ..., P are 'p' equally-sized [m - 1, q] matrices.
My goal is to reshape this matrix in a way that the new matrix 'X' is:
X = [A, B, C, ..., P]
Ideally, this would be done without loops or manually, so that the operation could be vectorised. A representative example to solve 'similar' to my case matrix would be:
m = 8;
p = 2;
q = 10;
X = rand(p * (m - 1), q);
X(:, [1, q]) = 0;
Which would be a random matrix made of 2 (p) submatrices of [7, 10] ([m - 1, q]) elements. The final solution that I am seeking would be, for this particular case:
Y = [X(1 : m - 1, :), X(m : 2 * (m - 1), :)];
Some help on how to do this transformation for a generic number of matrices 'p' of dimension [m - 1, q] via the reshape command, circshift, rot90, transpose or similar would be greatly appreciated, so that the transformation was compact.
Thanks in advance and regards,
Moreno, M.
0 Comments
Accepted Answer
DGM
on 10 Apr 2022
There are other ways this could be done, but just using reshape() and permute() is often the fastest:
m = 5;
p = 2;
q = 5;
X = rand(p * (m - 1), q);
X(:, [1, q]) = 0
% given example
Y = [X(1 : m - 1, :), X(m : 2 * (m - 1), :)]
% using reshape()
Y = reshape(permute(reshape(X.',q,m-1,p),[1 3 2]),[],m-1).'
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping 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!