Command to reshape matrix diagonally
2 次查看(过去 30 天)
显示 更早的评论
Hi, i'd like to transform
X = 1 100 40
2 107 30
3 90 50
4 120 60
into
Y = 1 100 40 3 90 50
2 107 30 4 120 60
I thought
Y = reshape(X,2,6)
would do the trick but it didn't give me the matrix I was looking for.
Any quick ideas will be appreciated.
Thanks!
0 个评论
采纳的回答
Andrei Bobrov
2016-4-8
编辑:Andrei Bobrov
2016-4-8
% X - array with size [5541888 x 7]
% m2 - number of rows in second array, m2 = 1536
[~,n] = size(X);
out = reshape(permute(reshape(X',n,m2,[]),[1 3 2]),[],m2)';
or
[m,n] = size(X);
a = mat2cell(X,m2*ones(m/m2,1),n);
out = [a{:}];
2 个评论
Andrei Bobrov
2016-4-8
rewrite on separate commands:
[~,n] = size(X);
a = reshape(X',n,m2,[]);
b = permute(a,[1 3 2]);
out = reshape(b,[],m2)';
更多回答(1 个)
另请参阅
类别
在 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!