0

I have the following equation in MATLAB:
eqn1 = (1-t)*x1 + t*x2 == x; where x1,y1 = <some_constant_value>
I am solving the equation as:
t1 = double(solve(subs(eqn1,x,min(x_arr(i,:))),t)); and doing a comparison as:

if(t1 >= 0 & t1 <= 1)
    crossing = 1;
    return
end

However, from time to time, I get the following error:
Operands to the || and && operators must be convertible to logical scalar values.
From what I found out on other forums/answers, this is because && and || is not capable of handling arrays and hence, the error. But, considering I am solving a linear equation, why is MATLAB returning an array?

EDIT
My apologies, I forgot to give the initialization for the variables:
syms x y t
x1 as double
x2 as double
y1 as double
y2 as double
x_arr as double
y_arr as double

Kanishka Ganguly
  • 1,252
  • 4
  • 18
  • 38
  • How do you initialize x1 and y1 and what does solve return? Maybe no solution is returned? – Daniel Mar 26 '16 at 07:57
  • 1
    please post the actual code, something we can test. – Amro Mar 26 '16 at 10:37
  • This is still not enough info for us to reproduce the problem (see [MCVE](http://stackoverflow.com/help/mcve). If I had to guess, perhaps `solve` found multiple solutions? Maybe you can modify your `if` test to use `all` and `any` functions depending on your application... – Amro Mar 27 '16 at 09:10
  • @Amro this is a self-sufficient function and runs independent of the rest of the code. The actual code is far too large to reproduce here, so I thought this would be enough. I thank you for your time and effort in trying to solve this problem. – Kanishka Ganguly Mar 27 '16 at 18:27
  • @Amro my only question was that why would `solve` return multiple solutions for a linear equation of one variable? Perhaps you could shed some light on that? – Kanishka Ganguly Mar 27 '16 at 18:28
  • that was just a wild guess, we didn't have much to go on.. The other possibility is that your actual code has a bug not depicted in your stripped-down example. Say are you using `&` or `&&` in the original if condition? – Amro Mar 27 '16 at 18:33
  • Since I want to check if `t` lies between `0` and `1`, I am using `&&`. But if I change it to `&`, I don't get the error. This confirms the fact that `solve` returns a vector, which is the confusing part. – Kanishka Ganguly Mar 27 '16 at 18:41
  • 1
    well you can always debug it by placing a breakpoint at that line and know for sure :) We don't have the full code, so only you can do it. MATLAB also has an option to break on error `dbstop if error` that comes in handy to debug errors. – Amro Mar 27 '16 at 19:05

1 Answers1

0

I got it working in a hacky manner by splitting the if conditions as:

if(t1 >= 0)
    if(t1 <= 1)
        crossing = 1;
        return
    end
end
Kanishka Ganguly
  • 1,252
  • 4
  • 18
  • 38