Reshape a 'column' matrix into a 'row' matrix
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
DGM
2022 年 4 月 10 日
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).'
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!