Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
IF loop returning wrong array values ?

Subject: IF loop returning wrong array values ?

From: DRG

Date: 7 Jul, 2011 01:12:44

Message: 1 of 7

Hey there. I have a code and it's all working fine, except one part.
There is a for loop between s = 1:800.
In the middle of this for loop, there is are IF loops to test
conditions. All these if loops also return array values of 800, except
one which produces an array which is otherwise correct but with only
532 values..

The IF loop in question works something like this;

IF the value is below a value z(s) AND positive, E = A value given by
a formula.

If the value is above z(s) but below x(s) AND positive, E = A
different given by a different formula.

If the value is aboe x(s), E = 0.

If the value is negative at any point, E = 0;

This should work for all cases, yet while all other arrays are 1 x
800, this one particular array keeps spitting out a 1 x 532 array. its
values inside are correct, but I have no idea why it isn't producing
all 800 when the other practically identical if loops are.

Any ideas ? I have appended the odd bit of code below (simplified...)

Nested in a FOR loop, which all functions of (s) defined....



if A(s).*(B(s) + C(s)) >= 0 && zf(s) <= z(s) %if function
returns positve value below z(s)

        E = A(s).*(B(s) + C(s)) ; % formula

    elseif A(s).*(B(s) + C(s)) >= 0 && zf(s) > z(s) && zf(s) <= X(s)
%beyond z(s), below x(s) positive..

         E(s) = G(s).*A(s).*(B(s) + C(s)) ;

    else

        E(s) = 0; % all other cases

    end

This produces an array with correct values but only 532 instead of
800... any ideas why and where missing values are gone ?

Subject: IF loop returning wrong array values ?

From: dpb

Date: 7 Jul, 2011 01:22:20

Message: 2 of 7

On 7/6/2011 8:12 PM, DRG wrote:
...

> This produces an array with correct values but only 532 instead of
> 800... any ideas why and where missing values are gone ?

Impossible to follow posting...post the code snippet intact w/o the
narrative (or at least make the narrative outside the code).

--

Subject: IF loop returning wrong array values ?

From: DRG

Date: 7 Jul, 2011 01:39:10

Message: 3 of 7

Sure, sorry... the actual code is very long, so just the if loop in
question for s = 1:800



if A(s) >= 0 && zf(s) <= z(s)

        E = A(s)

    elseif A(s) >= 0 && zf(s) > z(s) && zf(s) <= X(s)


         E(s) = G(s).*A(s)

    else


        E(s) = 0;

    end





On Jul 7, 2:22 am, dpb <n...@non.net> wrote:
> On 7/6/2011 8:12 PM, DRG wrote:
> ...
>
> > This produces an array with correct values but only 532 instead of
> > 800... any ideas why and where missing values are gone ?
>
> Impossible to follow posting...post the code snippet intact w/o the
> narrative (or at least make the narrative outside the code).
>
> --

Subject: IF loop returning wrong array values ?

From: Roger Stafford

Date: 7 Jul, 2011 03:35:10

Message: 4 of 7

DRG <grimesd2@gmail.com> wrote in message <8c2b699f-13b6-440b-8da6-e24b59c7c85b@o4g2000vbv.googlegroups.com>...
> Hey there. I have a code and it's all working fine, except one part.
> There is a for loop between s = 1:800.
> In the middle of this for loop, there is are IF loops to test
> conditions. All these if loops also return array values of 800, except
> one which produces an array which is otherwise correct but with only
> 532 values..
>
> if A(s).*(B(s) + C(s)) >= 0 && zf(s) <= z(s)
> E = A(s).*(B(s) + C(s)) ;
> elseif A(s).*(B(s) + C(s)) >= 0 && zf(s) > z(s) && zf(s) <= X(s)
> E(s) = G(s).*A(s).*(B(s) + C(s)) ;
> else
> E(s) = 0;
> end
>
> This produces an array with correct values but only 532 instead of
> 800... any ideas why and where missing values are gone ?
- - - - - - - - - -
  The line "E = A(s).*(B(s) + C(s));" is surely in error. It will erase all previous entries in E when it is performed. Any subsequent E-entries will have zeros in all the earlier s positions.

Roger Stafford

Subject: IF loop returning wrong array values ?

From: dpb

Date: 7 Jul, 2011 14:24:37

Message: 5 of 7

On 7/6/2011 8:39 PM, DRG wrote:
> Sure, sorry... the actual code is very long, so just the if loop in
> question for

s = 1:800
...
> if A(s)>= 0 && zf(s)<=z(s)
> E = A(s)

The above line references _all_ of E; is this intended?

> elseif A(s)>= 0 && zf(s)>z(s) && zf(s)<= X(s)
> E(s) = G(s).*A(s)

Assuming this is still above index variable s, the above line has no
need for the ".*" operator (doesn't hurt anything, but implies perhaps
misunderstanding of what are doing???) The dot-operator form is used
for element-by-element operation on arrays/matrices; E(s), G(s) and A(s)
will b single elements anyway.

> else
> E(s) = 0;
> end
...

It appears the above could be written as

E=zeros(size(A));
idx=A(s)>= 0 && zf(s)<=z(s);
E(idx) = A(idx);
idx = A(s)>= 0 && zf(s)>z(s) && zf(s)<= X(s);
E(idx) = G(idx).*A(idx);

(BTW, please don't toppost--hard follow conversation makes)...

--

Subject: IF loop returning wrong array values ?

From: dpb

Date: 7 Jul, 2011 15:05:49

Message: 6 of 7

On 7/7/2011 9:24 AM, dpb wrote:
...

> It appears the above could be written as
>
> E=zeros(size(A));
> idx=A(s)>= 0 && zf(s)<=z(s);
> E(idx) = A(idx);
> idx = A(s)>= 0 && zf(s)>z(s) && zf(s)<= X(s);
> E(idx) = G(idx).*A(idx);
...

Ooops, forgot to finish edit after cut 'n paste...

E=zeros(size(A));
idx=A>= 0 && zf<=z;
E(idx) = A(idx);

idx = A>= 0 && zf>z && zf<= X;
E(idx) = G(idx).*A(idx);

--

Subject: IF loop returning wrong array values ?

From: Walter Roberson

Date: 8 Dec, 2011 16:45:09

Message: 7 of 7

DRG <grimesd2@gmail.com> wrote in message <8c2b699f-13b6-440b-8da6-e24b59c7c85b@o4g2000vbv.googlegroups.com>...
> Hey there. I have a code and it's all working fine, except one part.
> There is a for loop between s = 1:800.
> In the middle of this for loop, there is are IF loops to test
> conditions.

There is no such thing as an if loop.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us