Here is the modified code:
P = [1, 0, 2];
AB = [1, 2, 3];
CD = [4, 5, 6];
EF = [7, 8, 9];
M = {[AB], [CD], [EF]};
for i = 1:length(M)
P1 = P(M{i}~=0);
t = M{i};
M1 = t(M{i}~=0);
P2 = P1(~isnan(M1));
M2 = M(~isnan(M1));
if any(cellfun(@(m) isequal(m, AB), M))
f = sum(plus(P2, M2));
elseif any(cellfun(@(m) isequal(m, CD), M))
f = sum(plus(P2, M2));
elseif any(cellfun(@(m) isequal(m, EF), M))
f = sum(plus(P2, M2));
end
end
The output in this case should be the addition of two matrices P2 and [AB] which is 9, P2 and [CD] which is 21, P2 and [EF] which is 27. However, it loops over the 1st if statement, and does not go to the elseif statement.