Reshape column vector

2019-01-28 08:44发布

Hello I'm working with MATLAB and I have a "z" column vector that has dimension of (9680 x 1). I want to reshape it in order to have an array "z" of dimension (44 x 220). I'm doing the following:

z=reshape(z,44,220);

I also tried:

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

But the output is not right (at least the first row). I can see it by comparing the output matrix with the initial 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 and so on till obtaining 44 rows.

What am I doing wrong? Thanks for your help.

2条回答
冷血范
2楼-- · 2019-01-28 09:46

Matlab stores the matrix values in column major format (this is important during reshape). Since you want row major, you need to do

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

i.e. transpose afterwards.

查看更多
够拽才男人
3楼-- · 2019-01-28 09:47

I'd use Andreas H.'s approach.

As an alternative, there's a vec2mat function in the Communications Toolbox that does just that, and even fills missing values if 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
查看更多
登录 后发表回答