&& in if statement returning error
조회 수: 53 (최근 30일)
이전 댓글 표시
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?
댓글 수: 1
Guillaume
2014년 10월 22일
Justin, you must have deleted your latest comment/answer while i was replying to it.
To make it easier to understand exactly what you want to test, could you give an example using real matrices.
채택된 답변
Guillaume
2014년 10월 22일
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, :);
댓글 수: 0
추가 답변 (4개)
Guillaume
2014년 10월 22일
&& can only operate on expressions that return scalar logical indeed. In addition it has a short-circuiting behaviour, meaning that if the left-hand side expression evaluate to false, it doesn't even evaluate the right-hand side expression (i.e. if it's a function, it's never called and its side effects don't happen).
& is an element-wise logical operator. It works on matrices and returns a matrix of the same size at the operands.
It's still not clear what you want to do,
d_0_ij(1:sz_dist_obs(1,1),1)
is going to be a column vector of some elements of the 1st coumn of d_0_ij, which you compare to the scalar distance_obs(s, 1).
If you want to compare two matrices (e.g. m1 and m2), your if statement should be:
if cond1 && all(m1 == m2)
댓글 수: 0
James Tursa
2014년 10월 22일
It appears you have a vector involved in your test, e.g.,
d_0_ij(1:sz_dist_obs(1,1),1) == distance_obs(1,1)
As written, if sz_dist_obs(1,1) is greater than 1 you will get a vector result for the indexing, and then subsequently a vector result for the == operation. What is your intention here? If you want all of the values in the vector to be equal to the right value of the == operation, then you can use the all function. E.g., this would result in a scalar:
all(d_0_ij(1:sz_dist_obs(1,1),1) == distance_obs(1,1))
And you will need to fix the other == operation as well. If this is not your intention, then you will need to clarify what you want for this test.
Julia
2014년 10월 22일
Hi,
I had some issues with &&, too.
I solved it with using only & (Matlab does not really like it, but it runs).
댓글 수: 5
Sushant Shetty
2014년 10월 22일
The conditions between && should have a logical output (boolean) Your 1st condition sz_dist_obs(1,1) will not output a boolean value. I guess this is why you are getting this error. In your MATLAB workspace you can see the difference between a normal scalar variable and a logical variable. Scalar variable will have a box like icon and logical variables have a "v" shaped icon.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!