MATLAB: How to replace values in matrix B with a constant at indices of matrix A for which the values in A meet a condition

matrix indexing

I have two 2D matrices A and B of the same size in each dimension (i.e., same number of rows and columns). I want to find the row and column indices of values in A that meet a condition, then replace the values in B at those indices with a constant value.
I tried the following which did not work, and instead replaced all of the values in B with the specified constant:
[r,c] = find(A >= 108);
B(r,c) = 555;
In trying this, about half of the elements in A were >=108 and half of the elements in A were <108, but all of the elements in B were replaced with 555.
I could do this with a for loop indexing through r and c like the following which worked when I tried it, but I am hoping to avoid the for loop since I am dealing with large matrices:
[r,c] = find(A >= 108);
rmax = size(r); %Note that r and c have the same length
for i = 1:rmax
B(r(i),c(i)) = 555;
end
Is there a more direct way than the for loop implementation to do this?

Best Answer

B(A >= 108) = 555