Rearrange Column Vector

Hello I am working with MATLAB and I have a column vector "z" whose size is (9680 x 1). I want to change it to have an array of "z" dimensions (44 x 220). I am doing the following:

z=reshape(z,44,220);

      

I've also tried:

z=reshape(z,[44,220]);

      

But the result is wrong (at least the first line). I can see this by comparing the output matrix with the start vector.

I just need the 220 first positions of the column vector to be the length of the first row of the matrix, then the next 220 positions of the vector to be the second row of the matrix, etc. to get 44 lines.

What am I doing wrong? Thanks for your help.

+3


source to share


2 answers


Matlab stores matrix values โ€‹โ€‹in the basic column format (this is important when changing the shape). Since you want a basic row of lines, you need to do

z = reshape(z, [220 44]).';

      



i.e. transpose afterwards.

+6


source


I would use Andreas H.'s approach .

Alternatively, there is a function vec2mat

in the Communication Toolbox that does exactly that, and even fills in missing values โ€‹โ€‹when needed:



>> x = 11:18;
>> vec2mat(x,4) %// no padding needed
ans =
    11    12    13    14
    15    16    17    18
>> vec2mat(x,5) %// padding needed; with 0 by default
ans =
    11    12    13    14    15
    16    17    18     0     0
>> vec2mat(x,5,-1) %// padding needed; with specified value
ans =
    11    12    13    14    15
    16    17    18    -1    -1

      

+2


source







All Articles