![]() |
如何附加到最终尺寸?
我从单独的文件加载多个结构,并希望将它们收集在一个数组结构中。由于该结构具有许多字段,因此我正在遍历其fieldnames ,如下所示:
for fnc = fieldnames(result)' fn = fnc{:} results.(fn) = [results.(fn) result.(fn)]; end 这对于标量字段很好用,但是某些字段是矢量或矩阵,因此应分别存储为矩阵或三阶张量。我可以使用一些if length和if ndims分别处理这些情况,但是 [INDENT]有没有更通用的方法来使用下一个更高的维度进行附加? [/INDENT] [B]回答:[/B] 要沿最后一个维度附加,可以使用命令[URL="http://www.mathworks.com/help/techdoc/ref/cat.html"]CAT[/URL]和[URL="http://www.mathworks.com/help/techdoc/ref/ndims.html"]NDIMS[/URL] : %# find the number of dimensions nd = ndims(results.(fn)); %# catenate along the last dimension results.(fn) = cat(nd,results.(fn), newResult.(fn)); 请注意,如果存在大量迭代,则通过分类构建数组可能会非常慢,尽管在您的情况下,预分配会有所涉及,因此,仅在确实有必要时才这样做。另外,使用变量results以及result都results自己陷入难以发现的错误中。 [url=https://stackoverflow.com/questions/5116324]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 02:59。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.