如何在MATLAB中连接向量以创建非矩形矩阵? [英] How can I concatenate vectors to create non-rectangular matrices in MATLAB?

查看:130
本文介绍了如何在MATLAB中连接向量以创建非矩形矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有创建非矩形矩阵的方法?例如,如果我有一个矩阵a=[6 8 10]和另一个矩阵b=[1 5],我可以垂直连接它们以便在一行中获取[6 8 10]并在另一行中获取[1 5]吗?

Is there a way to create non-rectangular matrices? For example, if I have a matrix a=[6 8 10] and another matrix b=[1 5], can I vertically concatenate them in order to obtain [6 8 10] in one row and [1 5] in another?

推荐答案

直接回答是. MATLAB不支持参差不齐或非矩形或非正方形的矩阵.解决这个问题的一种方法是制作一个单元格数组,其中每个单元格都是不等长的向量.

The direct answer is no. MATLAB does not support ragged or non-rectangular or non-square matrices. One way you could get around this is to make a cell array, where each cell is a vector of unequal lengths.

类似的东西:

a = [6 8 10];
b = [1 5];
c = cell(1,2);
c{1} = a;
c{2} = b;

celldisp(c)

c{1} =

     6     8    10

c{2} =

     1     5

另一种方法是创建一个矩阵,其中那些不包含任何值的值映射到预设数字,例如零.因此,可以将ab连接为矩阵,使其变为[6 8 10; 1 5 0];.如果这是您的首选,则可以执行以下操作:

Another way would be to create a matrix where those values that contain nothing get mapped to a preset number, like zero. Therefore, you could concatenate a and b to be a matrix such that it becomes [6 8 10; 1 5 0];. If this is what you prefer, you can do something like this:

a = [6 8 10];
b = [1 5];
c = zeros(2, 3);
c(1,1:numel(a)) = a;
c(2,1:numel(b)) = b;
disp(c)

 6     8    10
 1     5     0

可以在gnovice的答案中找到有关此特定主题的更全面的论述:

A more comprehensive treatise on this particular topic can be found in gnovice's answer: How can I accumulate cells of different lengths into a matrix in MATLAB?

乔纳斯(Jonas)创建了另一个相关的答案:如何我如何将不均匀的矩阵合并为一个矩阵?

Another related answer was created by Jonas: How do I combine uneven matrices into a single matrix?

这篇关于如何在MATLAB中连接向量以创建非矩形矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆