MATLAB: Finding places where an array are equal to zero and replacing them accordingly

arrayfind elements equal to a certain valueindexingreplacing values in an array

I have written some code, however, due to the fact that my alogirthm is really quite complicated it is hard for me to see if my code is working
I Have a 16x16x16 array and I want to find the places where that array = zero;
I have done this in the following way ind = A == 0
I then want to create a new array which takes values from one array at the places where it is zero and values from another else where; Would this code do it?
ANEW(ind)=Adashup2new(i,j,k);
ANEW(~ind) = MAT(i,j,k);

Best Answer

Try this:
% Initialize ANEW to MAT.
ANEW = MAT;
% Find zero values;
zeroIndexes = A == 0;
% Replace ANEW values at zeroIndexes with values from Adashup2new
ANEW(zeroIndexes) = Adashup2new(zeroIndexes);
Adashup2new must be the same dimensions as A, ANEW, and MAT.