If statement

View: New views
3 Messages — Rating Filter:   Alert me  

If statement

by A.M.Zainal-Abidin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


  Hello,

I am trying to create a stem plot for  integers  ranging
from 75 to 81. I tried using the if statements so that if
the integer is equal or larger than 80, the assigned
figure is 1 and if the figure is below or equal to 75,
then the assigned figure is zero. The rest in between will
be assigned 0.75 (or some arbitrary figure). the script is
as below.

%Mould risk for wall 1
w1=linspace(0,1,length((weeklyaverage1));
for i=1:length((weeklyaverage1));
  if (weeklyaverage1>=80)  w1(i)=1;
  elseif (weeklyaverage1<=75)  w1(i)=0;
  else w1(i)=0.75;
  endif
end
figure();
clf;
stem(w1);

However, the stem plot shows that all the figures assigned
at 0.75, meaning all the figures that I have are between
76 and 79. Is my script at fault here? I have tried using
switch statement but the stem plot still shows the same
pattern.

Thank you.

Murad
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: If statement

by James Sherman Jr.-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think that your if/elseif statements should be testing weeklyaverage1(i) not weeklyaverage1.  Though you can actually do this without the for loop at all by doing something similar to what you (possibly mistakenly) are doing now.

w1 = 0.75;
w1(weeklyaverage1>=80) = 1;
w1(weeklyaverage1<=75) = 0;

I think would do what you are looking to do without the need for the for loop.

On Thu, Oct 15, 2009 at 9:41 AM, <A.M.Zainal-Abidin@...> wrote:

 Hello,

I am trying to create a stem plot for  integers  ranging
from 75 to 81. I tried using the if statements so that if
the integer is equal or larger than 80, the assigned
figure is 1 and if the figure is below or equal to 75,
then the assigned figure is zero. The rest in between will
be assigned 0.75 (or some arbitrary figure). the script is
as below.

%Mould risk for wall 1
w1=linspace(0,1,length((weeklyaverage1));
for i=1:length((weeklyaverage1));
 if (weeklyaverage1>=80)  w1(i)=1;
 elseif (weeklyaverage1<=75)  w1(i)=0;
 else w1(i)=0.75;
 endif
end
figure();
clf;
stem(w1);

However, the stem plot shows that all the figures assigned
at 0.75, meaning all the figures that I have are between
76 and 79. Is my script at fault here? I have tried using
switch statement but the stem plot still shows the same
pattern.

Thank you.

Murad
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: If statement

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

tor, 15 10 2009 kl. 10:09 -0400, skrev James Sherman Jr.:
> I think that your if/elseif statements should be testing
> weeklyaverage1(i) not weeklyaverage1.  Though you can actually do this
> without the for loop at all by doing something similar to what you
> (possibly mistakenly) are doing now.
>
> w1 = 0.75;
> w1(weeklyaverage1>=80) = 1;
> w1(weeklyaverage1<=75) = 0;

You should initialise 'w1' to a vector with the same size as
'weeklyaverage1', i.e.

  w1 = repmat (0.75, size (weeklyaverage1));
  w1 (weeklyaverage1 >= 80) = 1;
  w1 (weeklyaverage1 <= 75) = 0;

Søren

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave