【发布时间】:2014-08-12 11:54:16
【问题描述】:
我想知道是否可以获得不同大小子数组的最小/最大值 在 matlab 中不使用循环。
% create a 1D vector with arbitory floating point values
A = rand(100,1,'double');
% get start indexes of sections of A (eg. A>0.9)
pos01 = A>0.9;
posIdx= [1;find(pos01);size(A,1)];
% if 1 or size(A,1) where not required, remove them
posIdx = unique(posIdx);
% min/max all sections:
for ix=1:size(posIdx,1)-1
Amin(ix) = min(A(posIdx(ix):posIdx(ix+1)));
Amax(ix) = max(A(posIdx(ix):posIdx(ix+1)));
end
如果您有一个非常大的向量 A 和很多部分,那么最后一行可能会非常慢。 我想知道如何在 matlab 中对这个循环进行矢量化。
我尝试使用 arrayfun、remap、bsxfun 等提出解决方案。 但是我能想到的所有解决方案都要求这些部分具有相同的大小 - 情况并非如此:(
有什么想法吗?
最好, 延斯·亨利克
【问题讨论】:
-
你真的需要在 last/fist 元素中重叠部分吗?
-
我可以避免重叠。 accumarray 正在完成这项工作。太好了。
标签: matlab max vectorization min arrays