&& in if statement returning error

45 Ansichten (letzte 30 Tage)
Jay
Jay am 22 Okt. 2014
Beantwortet: Guillaume am 22 Okt. 2014
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 Kommentar
Guillaume
Guillaume am 22 Okt. 2014
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 22 Okt. 2014
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, :);

Weitere Antworten (4)

Guillaume
Guillaume am 22 Okt. 2014
&& 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.
To convert a logical array into a scalar logical you use all (or any).
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)

James Tursa
James Tursa am 22 Okt. 2014
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.
  1 Kommentar
Jay
Jay am 22 Okt. 2014
Bearbeitet: Jay am 22 Okt. 2014
Sorry I made a typographical error.
if s <= sz_dist_obs(1,1) && d_0_ij(1:sz_dist_obs(1,1),1) == distance_obs(s,1) && d_0_ij (1:sz_dist_obs(1,1),2) == distance_obs(s,2)
I would like the if statement to match up values from two elements from the first matrix to two elements of the second matrix and populate the third matrix with the matching first and second elements and the third elements of matrix one.
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]

Melden Sie sich an, um zu kommentieren.


Julia
Julia am 22 Okt. 2014
Hi,
I had some issues with &&, too.
I solved it with using only & (Matlab does not really like it, but it runs).
  5 Kommentare
Guillaume
Guillaume am 22 Okt. 2014
@Justin: see my answer which has links to the documentation of & and &&
Jay
Jay am 22 Okt. 2014
Thankyou Sushant.
From this I take for my current utilisation I require the use of &.

Melden Sie sich an, um zu kommentieren.


Sushant Shetty
Sushant Shetty am 22 Okt. 2014
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.

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by