MATLAB: && in if statement returning error

I have the following if statement with multiple conditional statements:
where d_1(1,1,) = 6 % iteration limit
s = 1 % iteration count
for s = 1:d_1(1,1)
if s <= sz_dist_obs(1,1) && d_0_ij(1:sz_dist_obs(1,1),1) == distance_obs(1,1) && d_0_ij (1:sz_dist_obs(1,1),2) == distance_obs(1,2)
obs_dist_m(s,1) = distance_obs(s,1)
obs_dist_m(s,2) = distance_obs(s,2)
obs_dist_m(s,3) = distance_obs(s,3)
s=s+1
end
Returns the following error:
Operands to the and && operators must be convertible to logical scalar values.
I am assuming that because the condition after the && is not a scalar, but rather a conditional statement that this is where the problem lies.
Is this correct?
Is there another way of stipulating multiple conditions in the single if statement?

Best Answer

You wrote in a comment:
If both have the same specifiers (same element values along their respective rows) I would like the third matrix to state in column one the first specifying element of matrix 1, column two the second specifying element of matrix 1 and column three the specifying elements value of matrix 1.
For example:
M1 = [1,2,7.8; 3,7,12.1; 4,12,11.2; 9,6,17.4 ; 3,7,8]
M2 = [1,2,17; 3,2,21; 3,7,12; 9,7,18; 3,8,7 ; ]
M3 = [1,2,7.8; 3,7,8]
You can do that with a for loop:
m3row = 1;
for row = 1:size(M1, 1)
if M1(row, [1 2]) == M2(row, [1 2])
M3(m3row, :) = M1(row, :);
m3row = m3row + 1;
end
end
Note that the if statement is equivalent to
if all(M1(row, [1 2]) == M2(row, [1 2]))
and to
if M1(row, 1) == M2(row, 1) && M1(row, 2) == M2(row, 2)
However, ultimately, you don't need a loop:
matchrows = all(M1(:, [1 2]) == M2(:, [1 2]), 2); find all the rows of M1 and M2 that match in the first two columns
M3 = M1(matchrows, :);