如何在matlab中使用不同行数的不同矩阵创建矩阵?(How to create a matrix out of different matrices with different number of

编程入门 行业动态 更新时间:2024-10-07 06:39:01
如何在matlab中使用不同行数的不同矩阵创建矩阵?(How to create a matrix out of different matrices with different number of rows in matlab?)

我在matlab中有一个for循环,其中我想用不同行数的不同矩阵制作一个矩阵(我不知道行数的差异是多少)。 例如,我有两个矩阵,分别是10x1和4x1。 我如何从它们两个中制作一个矩阵。(在这种情况下,我知道有什么区别,但在我的循环中我不会知道它们的差异。)

A = 0.1000 0.5000 0.6000 0.8000 0.3000 0.2000 0.7000 0.6000 0.1000 1.0000 B = 0.1000 0.2000 0.3000 0.4000 C = 0.1000 0.1000 0.5000 0.2000 0.6000 0.3000 0.8000 0.4000 0.3000 NaN 0.2000 NaN 0.7000 NaN 0.6000 NaN 0.1000 NaN 1.0000 NaN

我希望我的最后一个矩阵像C一样。

I have a for loop in matlab, in which I want to make a matrix out of different matrices with different number of rows( I don't know what the difference of number of rows is gonna be). For example I have two matrices that are 10x1, and 4x1. How can I make a matrix from both of them.(In this case I know what is the difference, but in my loop I won't know their differences.)

A = 0.1000 0.5000 0.6000 0.8000 0.3000 0.2000 0.7000 0.6000 0.1000 1.0000 B = 0.1000 0.2000 0.3000 0.4000 C = 0.1000 0.1000 0.5000 0.2000 0.6000 0.3000 0.8000 0.4000 0.3000 NaN 0.2000 NaN 0.7000 NaN 0.6000 NaN 0.1000 NaN 1.0000 NaN

I want my last matrix to be like C.

最满意答案

双阵列

只要结果是矩形的,您就可以通过将其他矩阵连接在一起来在MATLAB中创建矩阵。 因此,根据您的示例,可以通过执行以下操作来生成新的矩阵/向量,因为连接会产生矩形输出:

u = rand(10,1); v = rand(4,1); newVector = [u;v];

但是,由于行数不同,您不能简单地将u和v连接到不同的列。 因此,需要填充某种类型以平衡行数:

newMatrix = [ u , [v ; zeros(length(u)-length(v),1)]];

其中不存在的v行已被0的向量填充。 如果想要NaN填充,只需将零向量乘以NaN 。 如果您不知道u或v长度是否更长,则可以执行以下操作:

maxRows = max([length(u),length(v)]); nFillRows = abs(length(u) - length(v)); % The fill vector will be empty if the vector has the maximum number of rows uColumn = [u ; zeros( nFillRows * (length(u) ~= maxRows) ,1)]; vColumn = [v ; zeros( nFillRows * (length(v) ~= maxRows) ,1)]; newMatrix = [uColumn,vColumn];

为了使其工作,需要知道要添加的行数。 这可以递归设置以使newMatrix更大和更大(尽管如果u或v是矩阵而不是向量,则可能需要使用size() ); 但是,动态增长的阵列可能会受到很大的影响。

细胞阵列

如果for循环的目的是生成您想要存储并稍后使用的向量列表,则单元数组可能是一个很好的数据结构:

vectorStorage = {u,v};

由于单元阵列的每个元素都可以拥有自己独立的数据类型,因此u和v具有不同的长度并不重要。 从命令行,您将看到:

>> vectorStorage = {u,v} vectorStorage = [10x1 double] [4x1 double]

第一个元素是向量u ,第二个元素是向量v 。 您可以通过在花括号中指定索引来调用存储的值: all(u == vectorStorage{1}) == 1 。

Double Arrays

You can create matrices in MATLAB from concatenating other matrices together as long as the result is rectangular. So, per your example, a new matrix/vector can be made by doing the following since the concatenation results in a rectangular output:

u = rand(10,1); v = rand(4,1); newVector = [u;v];

However, you cannot simply concatenate u and v into different columns since their row count differ. So, a fill of some sort is necessary to balance the number of rows:

newMatrix = [ u , [v ; zeros(length(u)-length(v),1)]];

where the non-existent rows of v have been filled by a vector of 0s. If you want a NaN fill, simply multiply the zero vector by NaN. If you don't know whether u or v has greater length, you can do the following:

maxRows = max([length(u),length(v)]); nFillRows = abs(length(u) - length(v)); % The fill vector will be empty if the vector has the maximum number of rows uColumn = [u ; zeros( nFillRows * (length(u) ~= maxRows) ,1)]; vColumn = [v ; zeros( nFillRows * (length(v) ~= maxRows) ,1)]; newMatrix = [uColumn,vColumn];

In order for this to work, the number of rows to add needs to be known. This can be set up recursively to make newMatrix larger and larger (though size() may need to be used if either u or v is a matrix instead of a vector); however, dynamically growing arrays can be a big performance hit.

Cell Arrays

If the for-loop's purpose is to generate a list of vectors that you simply want to store and use later, a cell array may be a good data structure to use:

vectorStorage = {u,v};

Since each element of a cell array can have it's own separate data type, it doesn't matter that u and v have different lengths. From the command line, you will see this:

>> vectorStorage = {u,v} vectorStorage = [10x1 double] [4x1 double]

The first element is vector u, and the second element is vector v. You can recall the value stored by specifying the index in curly braces: all(u == vectorStorage{1}) == 1.

更多推荐

本文发布于:2023-08-07 11:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   行数   如何在   matlab   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!