How to cut an array and reshape it while keeping the content in order?

조회 수: 11 (최근 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

채택된 답변

Star Strider
Star Strider 2016년 7월 5일
This is how I would do it:
T1 = [T1(:,1:11); T1(:,12:end)]
  댓글 수: 2
Joseph Schmidt
Joseph Schmidt 2016년 7월 5일
Do you know if there's a way to do this for an arbitrary sized matrix? I'm going to change the size of the original T1 to be a whole range of sizes. Say T1 is size 10x42 and I want to make it into size 20x21. I want a way to have it be reshaped but without me having to specify it each time.
Thank you, Joseph Schmidt
Star Strider
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개)

James Tursa
James Tursa 2016년 7월 5일
result = [T1(:,1:11);T1(:,12:22)];
  댓글 수: 1
Joseph Schmidt
Joseph Schmidt 2016년 7월 5일
Do you know if there's a way to do this for an arbitrary sized matrix? I'm going to change the size of the original T1 to be a whole range of sizes. Say T1 is size 10x42 and I want to make it into size 20x21. I want a way to have it be reshaped but without me having to specify it each time.
Thank you, Joseph Schmidt

댓글을 달려면 로그인하십시오.


Stephen23
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

Jos (10584)
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by