
270 Ñ Efficient Coding Using Vectorization Technique
else
C(i,j) = F(i,j);
end
end
end
Solution
From the code it can be observed that an element in matrix C is assigned the value of an
element in matrix D or matrix F depending on a condition. Consider the logical matrix
M = A > B which would contain 1 at places where the element of A is greater than the
element of B and 0 otherwise. If M(i,j) is 1, then we should assign D(i,j) to C(i,j) which
can be accomplished as C = D.* M. Similarly, if M(i,j) is 0, we should assign F(i,j) to
C(i,j), that is, C = F.* (~ M). Since M(i,j) is either 0 or 1, the final solution is as follows:
>> M = A > B;
>> C