Is there a quick way to add an array to another one of equal size, BUT only add if the particular element in the first array is 0?
For example, I start with Original = zeros(3)
I want to add 3 arrays to it. lets say
a = [1 2 3; 0 0 0; 0 0 0]
b = [0 0 0; 4 5 6; 0 0 0]
c = [0 0 0; 1 1 1; 7 8 9]
If I just add a, b, and c to Original I will get [1 2 3; 5 6 7; 7 8 9] but I want the second row of c to NOT add because that row of Original already has values in it.
So it should give Original = [1 2 3; 4 5 6; 7 8 9]
I am looking to use this with 3D arrays, and preferably without for looping through each element.

 Respuesta aceptada

Stephen23
Stephen23 el 8 de Sept. de 2019
Editada: Stephen23 el 8 de Sept. de 2019
>> d = cat(3,a,b,c);
>> sum(d.*(cumsum(d~=0,3)==1),3)
ans =
1 2 3
4 5 6
7 8 9

3 comentarios

Michael Luo
Michael Luo el 8 de Sept. de 2019
I really appreciate the quick answers, I was just wondering if this will work with 3D arrays?
And if not, can I just instead cat on the 4th dimension, and if so what would I need to change?
Thanks so much
Michael Luo
Michael Luo el 8 de Sept. de 2019
Ok so I just tested it and it does technically work if I concatenate in 4th dimension, however not as expected.
So what I'm trying to do is add layers of pixels to an image through a loop, each time adding a bit. To prevent white specks (adding a pixel twice) it must only add when the pixel is fully black (RGB = 0 0 0).
When I run using your code, I think some pixels are not added despite it being black. The resulting image still contains large chunks of black pixels.
Thanks again for the replies
Michael Luo
Michael Luo el 8 de Sept. de 2019
Turns out all I needed to do was tweak your code a tiny bit and it worked. Thanks!!!
Hope you have a good day

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Preguntada:

el 8 de Sept. de 2019

Comentada:

el 8 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by