# For Loop Iteration With Part of Vector

1 view (last 30 days)
Kelly McGuire on 9 Jul 2019
Commented: Kelly McGuire on 11 Jul 2019
I want to use a row vector to index in a for loop. But, I want to switch to a different function when the index reaches a certain element in the vector. My row vector (t) is 1x1271810. I would like to use my first function in a for loop up to element 945665 in the vector, and then use a different function for the rest of the for loop (or another for loop) from element 945666 to 1271810. Is this possible? Here is how I have my for loop written so far, how would I modify it?:
[a,b] = size(t);
for i = 1:a
for j = 1:b
PredCurrFun(i,j) = p(4).*((1-calcPinf(p,t(i,j),Co(i),Diff)).* ...
exp(-calcLambda(p,t(i,j),Co(i),Diff) .* t(i,j)) + calcPinf(p,t(i,j),Co(i),Diff));
end
end
"second function I would like to use is a 1 - exponential"

Charles DeLorenzo on 10 Jul 2019
Edited: Charles DeLorenzo on 10 Jul 2019
I would suggest using one for loop the length of 't' and a conditional statement to switch between the functions. As an example
for i=1:width(t)
if i<945665
% function 1
else
% function 2
end
end

Show 1 older comment
Kelly McGuire on 10 Jul 2019
The error is gone with this code, but I'm not sure if the second function is actually being used:
[a,b] = size(t);
for i = 1:a
for j = 1:b
if j <= 945665
PredCurrFun(i,j) = p(4) .* ((1 - calcPinf(p,t(i,j),Co(i),Diff)) .* exp(-calcLambda(p,t(i,j),Co(i),Diff) .* t(i,j)) + calcPinf(p,t(i,j),Co(i),Diff));
else
PredCurrFun(i,j) = exp(-calcLambda(p,t(i,j),Co(i),Diff) .* t(i,j));
end
end
end
end
Charles DeLorenzo on 11 Jul 2019
I'm glad I was able to get you a little further along.
I see why my first answer was confusing, since I did not include both dimensions of the array 't'. You did good by nesting the two for loops on i and j. My answer should have been
[a,b]=size(t)
for i=1:a
for j=1:b
if j<945665
% function 1
else
% function 2
end
end
end