For Loop Iteration With Part of Vector

2 ビュー (過去 30 日間)
Kelly McGuire
Kelly McGuire 2019 年 7 月 9 日
コメント済み: Kelly McGuire 2019 年 7 月 11 日
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
Charles DeLorenzo 2019 年 7 月 10 日
編集済み: Charles DeLorenzo 2019 年 7 月 10 日
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
  4 件のコメント
Charles DeLorenzo
Charles DeLorenzo 2019 年 7 月 11 日
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
as you already have.
It's hard for me to troubleshoot your code segment above without being able to run it, so I cannot provide a definitive answer on why the second function isn't being used. I would suggest using a conditional breakpoint to stop the if statement when j=945665 to see what's going on. Then, you can manually step the code to see if the second function is being calculated. Conditional breakpoints are under the 'Editor'-->'Breakpoints'-->'Set Breakpoint'. The 'Debug' menu will appear under 'Editor' after the code runs with the breakpoint.
Kelly McGuire
Kelly McGuire 2019 年 7 月 11 日
Great, thanks for the advice! That was very helpful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by