Re: Trouble using the OR operator within a 'while' loop...



On Mar 31, 4:35 pm, ImageAnalyst <imageanal...@xxxxxxxxxxxxxx> wrote:
On Mar 31, 11:17 am, matlab_rookie <matthewdodd...@xxxxxxxxxxxxx>
wrote:



On Mar 31, 4:09 pm, ImageAnalyst <imageanal...@xxxxxxxxxxxxxx> wrote:

On Mar 31, 10:57 am, matlab_rookie <matthewdodd...@xxxxxxxxxxxxx>
wrote:

On Mar 31, 3:27 pm, ImageAnalyst <imageanal...@xxxxxxxxxxxxxx> wrote:

On Mar 31, 9:39 am, matlab_rookie <matthewdodd...@xxxxxxxxxxxxx>
wrote:

Hi everyone,

I've been having some problems using a while loop in my code. I get
the error:

??? Error using ==> or
Matrix dimensions must agree.
Error in ==> letstryagain2 at 101
while totalconcentration < 50000 | totalconcentration2 < 50000

totalconcentration and totalconcentration2 are both within the while
loop, and generally totalconcentration2 is one step longer than
totalconcentration, but i'm not sure why.  I just can't work out how
to solve the problem! Whats happening at the moment, is that the while
loop only stops when the condition before the OR operator, but not for
the latter condition.

Any ideas would be greatfully accepted! As you can probably tell from
my name...i'm a bit of a newby at this!

----------------------------------------------------------------------
matlab_rookie :
I'm betting either totalconcentration or totalconcentration2 is a
multidimensional array.  That means that
totalconcentration < 50000
is also a multidimensional array.  If the dimensions of
totalconcentration and totalconcentration2 are not the same, you will
get that error.
But the main problem is that you're trying to create a NEW array by
OR'ing the two together when what I think you really want is the
logical OR operator ||.
Check out the following code:
clc;
a4 = magic(4)
a5 = magic(5)
b4 = a4 < 8 % 4 by 4 array.
b5 = a5 < 12 % 5 by 5 array.
b4ORedWithb5 = b4 | b5
logicalOrAB = b4 || b5;

Regards,
ImageAnalyst

Hi, thanks for getting back to me. I tried what you both suggested,
but i'm still having problems. When using the totalconcentration(end)
suggestion...the simulation just ran forever and didnt stop. Possibly
because i hadn't defined what the (end) was?

And when trying using the || operator, i get the error..."??? Operands
to the || and && operators must be convertible to logical scalar
values."

totalconcentration and totalconcentration2 are both 1 x k arrays. But
k seems to be one larger for totalconcentration2...- Hide quoted text -

- Show quoted text -

--------------------------------------------------------------------------- --------------------
matlab_rookie :
if they are both 1 x k arrays, then exactly what do you mean by:
while totalconcentration < 50000 | totalconcentration2 < 50000
You're comparing a whole array to a single number.  This will get you
a whole array as an answer.
Then is you use the OR operator, it can't OR those two arrays together
if they don't match up element-for-element.
Did you mean to just compare one particular element of the first array
to one particular element in the second array?
Did you mean to compare the max value in the first array to the max
value in the second array?
Explain what you want to do.
Regards,
ImageAnalyst

Yes i am wanting to compare the max value in the first array to the
max value in the second array. Basically what im doing is, i have 2
rovers navigating themselves to a chemical source. They detect the
chemical concentration after each step, compare it to the
concentration detected at the previous step, and move accordingly.
totalconcentration is the concentration detected by rover 1,
totalconcentration2 is that detected by rover 2. I'm wanting to run
the simulation until either of them detects a value of concentration
above '50000'.

I hope this is clear... sorry if this is a really trivial problem, i'm
still new to matlab and so im sometimes overlooking simple problems.

Thanks very much,
matlab_rookie- Hide quoted text -

- Show quoted text -

--------------------------------------------------------------------------- -------
Try this
while max(totalconcentration) < 50000 || max(totalconcentration2) <
50000

Note the double |.
Good luck,
ImageAnalyst

Thank again,

This has worked to a certain extent. Whats happening now is, the
simulation is running until both of the rovers have reached a value
above 50000....when really what im wanting is for the simulation to
stop once either of them has reached a value above 50000.

Matlab_rookie.
.



Relevant Pages