You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab如何将向量按成对元素转为矩阵?不使用reshape函数

Convert a 1D Vector to a 2D Matrix Without Using reshape

Hey there! I get that you want to turn your 1D coordinate vector into a 2-column matrix without relying on reshape—no problem, here are a few easy ways to do this in MATLAB:

Method 1: Direct Matrix Construction

You can manually slice the vector into pairs of elements and stack them as rows using semicolons. This is straightforward for small vectors like yours:

A = [1 2 3 4 5 6 7 8 9 10];
new_A = [A(1:2); A(3:4); A(5:6); A(7:8); A(9:10)];

After running this, new_A will be exactly the 5x2 matrix you're looking for.

Method 2: Initialize and Fill by Index

If you prefer a more scalable approach (in case your vector gets longer later), you can first create an empty matrix and fill it by assigning elements directly:

A = [1 2 3 4 5 6 7 8 9 10];
new_A = zeros(5, 2); % Initialize a 5x2 matrix of zeros
new_A(:) = A; % Fill the matrix column-wise with elements from A

MATLAB automatically fills matrices column-first when using the colon operator like this, which gives you the exact row-wise pairs you need.

Method 3: Loop Through Rows

For a more explicit approach, you can use a simple loop to assign each pair of elements to a row:

A = [1 2 3 4 5 6 7 8 9 10];
new_A = zeros(5, 2);
for i = 1:5
    new_A(i, :) = A(2*i - 1 : 2*i); % Grab elements (2i-1) to 2i for row i
end

This loop calculates the correct indices for each row's elements, making it clear what's happening at each step.

All these methods will produce the matrix you want without using reshape. Pick the one that makes the most sense for your use case!

内容的提问来源于stack exchange,提问作者Sabatino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:32:57