[英]Does MATLAB offer a more elegant way to iterate through 3D array to get 3rd dimension vectors?
I'm trying to iterate trough a fixed size 3d array in order to plot the 3rd vector dimension like this: 我正在尝试通过固定大小的3d数组进行迭代,以绘制第3个矢量尺寸,如下所示:
%respo is a 3D array of fixed size defined above
for ii = 1:size(respo,1)
for jj = 1:size(respo,2)
plot(squeeze(respo(ii,jj,1:8)))
end
end
Is there a better way to do this than by 2 level for loop with pointing exactly to the vector plotted at each iteration? 有没有比通过2级循环精确地指向每次迭代绘制的矢量更好的方法了?
I get there is a linear indexing for each array in MATLAB, but I struggle to come up with a way that saves from the double-looping. 我在MATLAB中为每个数组建立了线性索引,但是我想出了一种避免双重循环的方法。
Well I guess you could reshape it to only need one loop: 好吧,我想您可以将其重塑为只需要一个循环:
respo_2D = reshape(respo, [], size(respo,3))
So now 所以现在
for ii = 1:size(respo_2D, 1)
plot(respo(ii,1:8));
end
(or potentially even plot(respo_2D(:,1:8)')
depending on what you're trying to do) (甚至可能根据您要执行的操作
plot(respo_2D(:,1:8)')
)
plot
applied to a matrix plots the columns of that matrix. plot
应用于矩阵图是矩阵的列。 So: rearrange dimensions such that the third becomes the new first and the others get combined into the new second, and then just call plot
因此:重新排列尺寸,以使第三个成为新的第一个,其他合并成新的第二个,然后调用
plot
plot(reshape(permute(respo, [3 1 2]), size(respo,3), []))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.