Turning iteratively all elements after and above a '1' into '0'-s starting from the left bottom corner
1 次查看(过去 30 天)
显示 更早的评论
I have logical square matrices consisting of non-intersecting rectangular patches of '1'-s with no gaps and otherwise '0'-s. 'No gaps' in particular means that two different rectangular patches are not allowed to share horizontal or vertical coordinates. Moreover, the rectangular patches are strictly monotone increasing from the POV of the bottom left corner. For example:
A = [0 0 0 0 1;
0 0 0 0 1;
0 1 1 1 0;
0 1 1 1 0;
1 0 0 0 0]
I would like to perform the following operation on A in a fast way, possibly on GPU. Starting from the left bottom corner, I would like to iteratively turn all elements above and after a '1' into '0'. For example:
B = [0 0 0 0 0;
0 0 0 0 1;
0 0 1 0 0;
0 1 0 0 0;
1 0 0 0 0]
That is, B(3,3) = 1 because after the iteration at A(4,2), A(4,3) turns into a '0'.
My logical matrices are of size at most 15 and I would like to perform this operation on a 3D-array of many such square matrices stacked upon each other in a fast way, hence the request for vectorizability if possible.
7 个评论
采纳的回答
DGM
2022-4-13
编辑:DGM
2022-4-13
This might not be particularly efficient, but maybe it's one way (if i'm interpreting it right):
A = [0 0 0 0 0 0 1
0 0 0 0 0 0 1;
0 0 0 0 1 1 0;
0 0 0 0 1 1 0;
0 1 1 1 0 0 0;
0 1 1 1 0 0 0;
1 0 0 0 0 0 0];
D = rot90(hankel(size(A,1):-1:1),2).*A;
D(D==0) = NaN;
Dmin = min(D,[],2);
D = D - (flip(cummax(flip(Dmin)))-1);
output = D==1
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!