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
Marin Genov
Marin Genov 2022년 4월 13일
편집: Marin Genov 2022년 4월 13일
@Matt J Gosh, nothing is going right today. I was implicitly counting from bottom to top, sorry about that. It's been a long day. I fixed now the coordinates in the example to be standard matrix coordinates. The rastering direction is diagonal so to speak. Basically, you are jumping from lower left corner to lower left corner, patch by patch. Also, the matrix is square.
@James Tursa: Because A(4,2) = 1. The zeroing procedure is simultaneous in the column above the 1 and the row after the 1. The important thing is that the patches are rectangular and contain no gaps of 0-s. So, if you start at lower left corner and turn everything above and eveything on the RHS into 0, you are left with a strictly smaller patch, 1x1 patch counts too.
I hope that makes more sense now.
Marin Genov
Marin Genov 2022년 4월 13일
@Matt J: Oh, I see what you mean by rastering direction. I added the clarification that the 'no gaps' condition also means that different rectangular patches do not share vertical or horizontal coordinates.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
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
output = 7×7 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by