How to change the step value of a for loop during execution
4 views (last 30 days)
Show older comments
Hey,
Im a little bit hung up over a bit of code that works on datasets of the form below.
x = Nan Nan Nan Nan 1 1 1 1 Nan Nan Nan 1 1.
most of the elements are nans with 2 segments containing numbers. These segments are not uniformly located in all the datasets
my code tries to do the following:
- look through each vector and save the first and last values of each contiguos segment of numbers.
To do this I use a combination of loops. its probably not the smartest way to solve the problem but there it is. the code works by using a for loop to scroll through the vector and handing control to a nested loop when it finds a number. - the nested loop saves the first and last values of the number segment and then hands back control to the main for loop. however I cannot change the step value of the for loop to skip over to the index where the nested loop left off. any ideas?
j = 1; for i=1:j:length(x) <--- how do I change the value of j?
while x(i) is a nan
j=1
continue
else result(1) = x(i)
for k = i:length(x)
if x(k) is nan
result(2) = x(k-1)
j = k - i
break(pass control back to main loop which attempts to skip to where this loop left off)
end
end
end
end
any help is highly aprreciated.
0 Comments
Accepted Answer
Matt Fig
on 22 Apr 2011
You cannot change this once the loop starts. Instead use a WHILE loop and have the increment depend on an IF statement.
More Answers (1)
Paulo Silva
on 22 Apr 2011
Here's a code that might work!
Save this function
function b=FindFirstAndLast(v)
%v is a line vector (1xN)
%the output is also a line vector with pairs of values
%odd indexes are the first element of a group of numbers
%even indexes are the last element of a group of numbers
%If a group is composed of just a number it will appear twice
%that means the number is the first and last element
b=zeros(1,numel(v)); %pre-allocate storage
c=0;d=0; %inicialize counter and index
for a=1:numel(v) %do it for all elements of the vector
if isnan(v(a)) % check if current value is nan
if c>0 %if we are inside a group of numbers
d=d+1; %increment index
b(d)=v(a-1); %save last segment value
end
c=0; %end of group of numbers reached so we reset the counter
elseif (c==0) %check if we have the first number of a group
c=c+1; %increment the counter
d=d+1; %increment index
b(d)=v(a); %save first segment value
end
end
%the vector might end with a number so the code won't save the last element
if ~isnan(v(a)) %we need to check if last value is also a number
d=d+1; %increment index
b(d)=v(a); %last value is also a number so save it
end
b=b(1:d); %discard not used indexes
And try it with the code
x=[nan nan nan nan 1 2 3 4 nan nan nan 5]; %sample vector
FindFirstAndLast(x)
See Also
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!