Matlab 3D matrix same index condition

1 view (last 30 days)
Hello,
Imagine two squared arrays A and B. I want array C to equal array A if array A is >= than array B.
Now, imagine array A is a 3D array. So I would need array C to also be 3D.
My knowledge is really limited, so any thought process (how you came up with the code) is welcome!
This is what I have (but it is not working), what can I change?
for k=1:1:15
for j=1:1:384
for i=1:1:384
if dBZ(i,j,k) >= dBZ_Mask(i,j)
weather(i,j,k) = dBZ(i,j,k);
end
end
end
end

Accepted Answer

Guillaume
Guillaume on 6 May 2017
It's not clear what C should be when A is smaller than B.
Option 1: C should be B when A is smaller than B. This is trivially achieved with the max function:
C = max(A, B);
Option 2: C should be zero (or another constant) when A is smaller than B. This is easily achieved with some simple comparison and logical indexing:
C = zeros(size(A)); %initialise to constant
C(A>=B) = A(A>=B); %copy values when A >= B

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!