How to step over a loop and go into next iteration & plot graph with different vector length?

4 views (last 30 days)
Hi,
I have a case which is something like this:
Let's say I have a variable X(i), in a while loop which increases in the multiple of 0.01, means that it's 0.01*i.
For each iteration i, I should be getting a value Y(i) at the end of the iteration.
However, for some of the values X(i), it's not possible in obtaining a value Y(i). So i'd like to step over and go into the next iteration, which is i+1. May I know how do I do that?
Also, at the end of the while loop, i'll need to plot a graph of Y vs X. But because i'm stepping over some X values, which means I'm not getting any values for Y, in the end i'll be getting X and Y of different vector length. In this case, I couldnt get a plot of the graph.
Any ideas how do i do this?
Sorry i couldnt post the code here because it is kind of long.
Hope anyone out there can help me with this.
Thanks

Answers (1)

Mahdi
Mahdi on 3 Apr 2013
Edited: Mahdi on 3 Apr 2013
Why don't you use the interp1() function to get data sets with the same number of points?
If it's not possible, I would suggest using the continue function in the loop.
A simple example would be:
X=[1 2 3 4 5 6 7 8 9]
for s=1:10
if ... % If you can evaluate y
Y(s)=s
else % If you can't evaluate it
X(s)=[] % Removes the value from the x matrix
continue
end
end
Just a simple example to see how the continue command works
for s=1:10
if s==8
continue
end
i
end
In the above example, we told the loop that if we reach the number 8, then just go back to the beginning of the loop. Running it will show i from 1 to 10, while skipping 8.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!