MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   简单的2D单元阵列附加 (https://www.labfans.com/bbs/showthread.php?t=26554)

poster 2019-12-14 20:13

简单的2D单元阵列附加
 
我有一个二维单元阵列。我要执行以下操作:

y = some_number; row(x) = [row(x) another_row(y)]; 但是,直到发生这种情况才定义row(x),所以行不起作用!当未定义row(x)时,如何简单地将another_row(y)追加到row(x)上?

抱歉,使用其他语言很容易做到这一点,但是我不确定在MATLAB中如何实现!

谢谢。



[B]回答:[/B]

您可以首先将row初始化为空数组(或单元格数组),如下所示:

row = []; %# Empty array row = {}; %# Empty cell array 然后,您可以像这样将新行追加到数组(或将新单元格追加到单元格数组):

row = [row; another_row(y)]; %# Append a row to the array row = [row; {another_row(y)}]; %# Append a cell to the cell array 有关[URL="http://www.mathworks.com/help/techdoc/math/f1-84864.html"]创建和连接矩阵的[/URL]更多信息,请参见文档。

还应注意,像这样的增长阵列不是很有效。假设您知道[URL="http://www.mathworks.com/help/techdoc/matlab_prog/f8-784135.html"]数组[/URL]的最终大小,则[URL="http://www.mathworks.com/help/techdoc/matlab_prog/f8-784135.html"]预分配数组[/URL]是一个更好的主意。如果您不知道最终大小, [URL="https://stackoverflow.com/questions/1548116/matrix-of-unknown-length-in-matlab/1549094#1549094"]则以块形式分配数组元素[/URL]可能会比一次将它们分配一次更有效。



[url=https://stackoverflow.com/questions/5250265]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 14:56

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.