简体   繁体   English

迭代函数输出到矩阵中

[英]Iterative function outputs into matrices

I have an iterative function which gives me two vector outputs. 我有一个迭代函数,可以给我两个向量输出。 How can I store these outputs into two separate matrices in Matlab? 如何在Matlab中将这些输出存储到两个单独的矩阵中?

[A, B]=iterative_function(x,y)

The size of A and B does not vary, they are 1x4. A和B的大小不变,为1x4。

Is there a way to do this without 'cell'? 没有“单元格”,有没有办法做到这一点?

If I understood correctly you want to concatenate all the A 's and B 's that are output at every iteration. 如果我理解正确,则希望将每次迭代输出的所有AB连接起来。 If their size never change you don't need to use cell arrays, you can use a regular matrix. 如果它们的大小从未改变,则不需要使用单元格数组,则可以使用常规矩阵。
Your A and B are row-vectors, so let's say you want to concatenate each result as a single row. 您的AB是行向量,因此,假设您要将每个结果串联为一行。

A_total=[];
B_total=[];
while(your loop is going)
    [A, B]=iterative_function(x,y)
    A_total=[A_total ; A];
    B_total=[B_total ; B];
end

If you know a priori how many iterations will be done by your loop you can as well preallocate such matrices A_total and B_total and assign each A and B to the i -th row: 如果您事先知道循环将执行多少次迭代,则还可以预分配这样的矩阵A_totalB_total并将每个AB分配给第i行:

A_total=zeros(MaxIteration,4); %because A has length 4
B_total=zeros(MaxIteration,4); %because B has also length 4
for i=1:MaxIterations
    [A, B]=iterative_function(x,y)
    A_total(i,:)=A;
    B_total(i,:)=B;
end

Update : as @Max correctly pointed out, I reckon is also worth knowing that you can do the very same thing (ie concatenate several vectors of equal size in a matrix) even if your vectors are not row-vectors but column-vectors. 更新 :正如@Max正确指出的,我想也值得知道,即使您的向量不是行向量而是列向量,您也可以做同样的事情(即在矩阵中连接多个大小相等的向量)。 The main trick is that now you do not assign 1 row = 1 vector, but 1 column = 1 vector. 主要技巧是现在不分配1行= 1个向量,而是1列= 1个向量。 Let's suppose that A and B are not 1x4, but 4x1. 假设AB不是1x4,而是4x1。 Such concatenation can easily be done as follows (inside the loop): 这样的连接可以很容易地通过以下方式完成(在循环内部):

    A_total=[A_total , A]; % a space without a comma works just as well
    B_total=[B_total , B];

Or, again, if you know a priori the number of iteration you can preallocate in this manner: 或者,再次,如果您知道先验的迭代次数,则可以通过这种方式预分配:

A_total=zeros(4,MaxIteration); %because A has length 4
B_total=zeros(4,MaxIteration); %because B has also length 4

this time you'll have as many columns as there are iterations whereas in the previous case you had as many rows as there were iterations. 这次,您将拥有与迭代一样多的列,而在前一种情况下,您将拥有与迭代一样多的行。 And inside the loop you can replace the i-th column in this manner: 在循环内部,您可以按以下方式替换第i列:

    A_total(:,i)=A;
    B_total(:,i)=B;

where (my bad I didn't explain earlier) the colon operator ( : ) means "all of them" (ie the expression A_total(:,i) means the i-th column and all the rows). 其中,(我的坏我没有解释更早)冒号运算符( : )表示“所有的人”(即表达A_total(:,i)表示第i列和所有行)。

Finally, both of these methods work because A and B have the same size and it'll never change. 最后,这两种方法都有效,因为AB具有相同的大小,并且永远不变。 Although if, for any reasons, such sizes turn out to be different, Matlab will not allow you to concatenate such vectors and your only chance relies on cell arrays. 尽管如果由于某种原因导致大小不同,Matlab将不允许您连接这些向量,并且唯一的机会取决于细胞阵列。 A matrix, as you'd expect, can only be created if all vectors have the same size (ie you cannot concatenate an array of length 4 and an array of length 5 in two rows). 正如您所期望的,只有在所有向量都具有相同大小的情况下才能创建矩阵(即,您不能在两行中连接长度为4的数组和长度为5的数组)。 Contrarily, a cell array is an heterogeneous data structure so each cell can contain whatever data you like. 相反,单元格数组是异构数据结构,因此每个单元格都可以包含您喜欢的任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM