×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

crazy loop

crazy loop

crazy loop

(OP)
Hi Guys
Have a look at the following simple matlab code:

x=[1;2;3;4;5;6;7;8;9;10];
y=[5;4;3;6;7;8;5;4;3;2];
z=[1;2;-7;-4;5;-1;4;-9;-6;5]

x1 = zeros(m, 1);% initialise output matrix
y1 = zeros(m, 1);
z1 = zeros(m, 1);

 for i = 1:m;
         x1(i) = x(i);
         y1(i) = y(i);
         z1(i) = z(i);

      if z1(i)<0        % to choose the negative elements
      if z1(i+1)>0      % if the next element is positiv
         plot(x(i),y(i),'o')
         hold on
     end
   end
end
------------------

I want to plot x versus y when z sign change from negative to positive ( z passes through zero). Matlab does not consider the second loop where I try to examine the following element. The answer is probably easy and silly but I spent few days without any success.

Any idea or suggestion??

Help is greatly appreciated.

Regards

Star

RE: crazy loop

(OP)
Sorry I forgot to define m, the code should be:

x=[1;2;3;4;5;6;7;8;9;10];
y=[5;4;3;6;7;8;5;4;3;2];
z=[1;2;-7;-4;5;-1;4;-9;-6;5]

m = size(x, 1);

x1 = zeros(m, 1);% initialise output matrix
y1 = zeros(m, 1);
z1 = zeros(m, 1);

 for i = 1:m;
         x1(i) = x(i);
         y1(i) = y(i);
         z1(i) = z(i);

      if z1(i)<0        % to choose the negative elements
      if z1(i+1)>0      % if the next element is positiv
         plot(x(i),y(i),'o')
         hold on
     end
   end
end

RE: crazy loop

Your problem is

CODE

if z1(i+1)>0
Your looking one sample ahead of the computation and according to

CODE

z1 = zeros(m,1)
this sample is always zero.

Something like this should solve the problem.

CODE

for i = 2:m
   if (z1(i-1)<0 & z1(i)>0)
      plot(....)
   end
end

RE: crazy loop

(OP)
Thanks Jockek.

That really works nicely.

Well done!!!!
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources