How to cut an array and reshape it while keeping the content in order?
12 次查看(过去 30 天)
显示 更早的评论
I have an array similar to what's below. How do I cut and move it so that after the 11th column (were it begins to go to 1 again) so that it's a 10x11 matrix instead of 5x22?
The reshape function doesn't work as it keeps the 1st column, pushes the 2nd column down and below the 1st column, keeps the 3rd column, and pushes the 4th column below the 2nd column. It messes up the data. Basically I want to cut the latter half of the data from column 12:end and place it below the 1:11 columns.
(5x22) T1 =
1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0
(10x11)T1 =
1 1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
0 个评论
采纳的回答
Star Strider
2016-7-5
This is how I would do it:
T1 = [T1(:,1:11); T1(:,12:end)]
2 个评论
Star Strider
2016-7-5
My pleasure.
The easiest way is to use the size function to define the column length.
This works with your ‘T1’ matrix, and should work for the others:
T1 = [T1(:,1:fix(size(T1,2)/2)); T1(:,fix(size(T1,2)/2)+1:end)]
The fix call may not be absolutely necessary, but I always include it to prevent problems.
更多回答(3 个)
Stephen23
2016-7-6
编辑:Stephen23
2016-7-6
This code can easily be changed to split the matrix into two, three, or more blocks. It assumes that the number of columns is divisible by this number of blocks.
>> blk = 2;
>> tmp = reshape(T1,size(T1,1),[],blk);
>> reshape(permute(tmp,[1,3,2]),[],size(tmp,2))
ans =
1 1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0
Example
We first define a function:
function out = moveblock(mat,blk)
tmp = reshape(mat,size(mat,1),[],blk);
out = reshape(permute(tmp,[1,3,2]),[],size(tmp,2));
end
and test this on a matrix:
>> T = ones(3,1)*(1:12)
T =
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
>> moveblock(T,2)
ans =
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
7 8 9 10 11 12
7 8 9 10 11 12
7 8 9 10 11 12
>> moveblock(T,3)
ans =
1 2 3 4
1 2 3 4
1 2 3 4
5 6 7 8
5 6 7 8
5 6 7 8
9 10 11 12
9 10 11 12
9 10 11 12
>> moveblock(T,4)
ans =
1 2 3
1 2 3
1 2 3
4 5 6
4 5 6
4 5 6
7 8 9
7 8 9
7 8 9
10 11 12
10 11 12
10 11 12
0 个评论
Jos (10584)
2016-7-6
This is easy using a cell array as an intermediate step
T = repmat(1:12,3,1) % example array
S = size(T)
n = 3
if mod(S(2),n)>0
C = mat2cell(T, S(1), repmat(S(2)/n,1,n))
Tout = cat(1,C{:})
else
disp('Not possible.')
Tout = []
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!