首页 > 解决方案 > (Matlab) Transform a vector into matrix by keeping the numbers by pairs

问题描述

Let's say I have a vector that contains some coordinates (x,y) in this way:

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

And I want to transform it in this matrix:

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

How can I do it? 'reshape' command is not what I'm searching. Thanks for the help.

标签: matlabmatrixvectorreshape

解决方案


Reshape 按列填充输出数组,因此您希望将其整形为 2x5 数组,然后转置:

>> A=[1 2 3 4 5 6 7 8 9 10];
>> reshape(A,2,[]).'
ans =
    1    2
    3    4
    5    6
    7    8
    9   10

推荐阅读