Reshape 59x16 to 236x4?

How can I change a matrix in MATLAB, preferably using reshape

?

Easy Matrix Installation:

A = [1 4 7 10; 2 5 8 11; 3 6 9 12]

      

what i want to change in

B = [1 4; 2 5; 3 6; 7 10; 8 11; 9 12]

      

I have tried many settings reshape

, but I cannot figure it out.

1 2 3 4
5 6 7 8

      

changed to

1 2
5 6
3 4
7 8

      

+3


source to share


3 answers


If I understand the transformation correctly, it is:

A = [1 4 7 10; 2 5 8 11; 3 6 9 12]
B = A(:,1:end/2);
B = [B;A(:,end/2+1:end)];

      

Is it correct?



EDIT: Or a general case:

function [B] = elefaaant(A,n)
[a,b] = size(A);
if mod(b,n) ~= 0
    error('Cannot reshape')
end
B = zeros(a*n,b/n);
fac = b/n;
for i = 1:n
    B((i-1)*a+1:i*a,:) = A(:,(i-1)*fac+1:i*fac);
end

      

+3


source


You can use reshape

and permute :

reshape(permute(reshape(A,size(A,1),2,[]),[1 3 2]),[],2)

      



Thanks to @LuisMendo who suggests modifying the answer to avoid depending on size A

.

+7


source


B = A(:,1:end/2);
B = [B;A(:,end/2+1:end)];

C = B(:,1:end/2);
C = [C;B(:,end/2+1:end)];

      

Perhaps it can be done in a simpler way, but it seems to work.

0


source







All Articles