更新时间:2023-08-20 15:45:28
一种方法是使用accumarray
.不幸的是,为了做到这一点,我们首先需要标记"您的逻辑矩阵.如果您没有图像处理工具箱,这是一种复杂的操作方式:
One approach is to use accumarray
. Unfortunately in order to do that we first need to "label" your logical matrix. Here is a convoluted way of doing that if you don't have the image processing toolbox:
sz=size(myLogicals);
s_ind(sz(1),sz(2))=0;
%// OR: s_ind = zeros(size(myLogicals))
s_ind(starts) = 1;
labelled = cumsum(s_ind(:)).*myLogicals(:);
所以Shai的bwlabeln
实现就是这样做的(但是形状是1
-by- numel(myLogicals)
,而不是形状的size(myLogicals)
)
So that just does what Shai's bwlabeln
implementation does (but this will be 1
-by-numel(myLogicals)
in shape as opposed to size(myLogicals)
in shape)
现在您可以使用accumarray
:
accumarray(labelled(myLogicals), myArray(myLogicals), [], @max)
否则尝试起来可能会更快
or else it may be faster to try
result = accumarray(labelled+1, myArray(:), [], @max);
result = result(2:end)
这是完全矢量化的,但是值得吗?您必须针对您的循环解决方案进行速度测试才能知道.
This is fully vectorized, but is it worth it? You'll have to do speed tests against your loop solution to know.