Collapsing matrix in columns

I have a 2D-matrix, where the number of columns is always a multiple of 3 (for example 250×27) - due to the repetitive organization results ( A,B,C, A,B,C, A,B,Cetc.). I want to modify this matrix to create a new matrix with three columns - each of which contains aggregated data for each type ( A,B,C) (for example, 2250×3).

So, in the matrix, 250×27all the data in the columns 1,4,7,10,13,16,19,22,25will be combined to form the first column of the resulting modified matrix.

The second column in the resulting reconfigured matrix will contain all the data from the columns 2,5,8,11,14,17,20,23,26- etc.

Is there an easy way to do this in MATLAB? I only know how to use reshapeit if the columns that I wanted to merge were adjacent ( 1,2,3,4,5,6) and not non-adjacent ( 1,4,7,10,13,16), etc.

+4
source share
8 answers

Shameless thief from @Divakar :

B = reshape( permute( reshape(A,size(A,1),3,[]), [1,3,2]), [], 3 );
+3
source

You can simply treat each set of columns as a separate item and make three changes together. This should do the trick:

[save as file "reshape3.m" in your Matlab folder to call it as a function]

function out = reshape3(in)
    [~,C]=size(in); % determine number of columns
    if mod(C,3) ~=0 
        error('ERROR: Number of rows must be a multiple of 3')
    end

    R_out=numel(in)/3; % number of rows in output

    % Reshape columns 1,4,7 together as new column 1, column 2,5,8 as new col 2 and so on
    out=[reshape(in(:,1:3:end),R_out,1), ...
        reshape(in(:,2:3:end),R_out,1), ...
        reshape(in(:,3:3:end),R_out,1)];
end
+2
source

A - . , : ( , , ).

A = rand(27); %as test
B = A(:,1:3:end);
C = A(:,2:3:end);
D = A(:,3:3:end);

reshape:

B = reshape(B,[],1);
C = reshape(C,[],1);
D = reshape(D,[],1);

, , :

A = [B C D];
+1

, 3x6 A

A = [1 2 3 4 5 6;6 5 4 3 2 1;2 3 4 5 6 7]
A =

     1     2     3     4     5     6
     6     5     4     3     2     1
     2     3     4     5     6     7

b =size(A)

c1 = A((1:b(1)),[1:3:b(2)])
c2 = A((1:b(1)),[2:3:b(2)])
c3 = A((1:b(1)),[3:3:b(2)])

A_result = [c1(:) c2(:) c3(:)]

A_result =

     1     2     3
     6     5     4
     2     3     4
     4     5     6
     3     2     1
     5     6     7
+1

2 :

nRows = size(matrix, 1);
nBlocks = size(matrix, 2) / 3;
matrix = reshape(matrix, [nRows 3 nBlocks]);
matrix = permute(matrix, [1 3 2]);
matrix = reshape(matrix, [nRows * nBlocks 1 3]);
matrix = reshape(matrix(:), [nRows * nBlocks 3]);
+1

2 :

rv  = @(x) x(:);
ind = 1:3:size(A,2);
B   = [rv(A(:,ind)) rv(A(:,ind+1)) rv(A(:,ind+2))];

reshape s, , .

0

If you have an Image Processing Tool , this is a very convenient solution: im2col

out = im2col(A,[1 4], 'distinct').'
0
source

Try Matlab function mat2cell , I think this form is allowed.

X is the "start matrix"    
C = mat2cell(X, [n], [3, 3, 3]); %n is the number of rows, repeat "3" as many times as you nedd

%extract every matrix
C1 = C{1,1}; %first group of 3 columns
C2 = C{1,2}; %second group of 3 columns 
%repeat for all your groups

%join the matrix with vertcat
Cnew = vertcat(C1,C2,C3); %join as many matrix n-by-3 as you have
-2
source

Source: https://habr.com/ru/post/1660282/


All Articles