Turning iteratively all elements after and above a '1' into '0'-s starting from the left bottom corner

1 visualización (últimos 30 días)
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 comentarios
Marin Genov
Marin Genov el 13 de Abr. de 2022
Editada: Marin Genov el 13 de Abr. de 2022
@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 el 13 de Abr. de 2022
@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.

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 13 de Abr. de 2022
Editada: DGM el 13 de Abr. de 2022
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

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by