How to convert a For loop function into an If function

3 次查看(过去 30 天)
I have a question. As i am using a MATLAB function block. I cannot use recursive functions like for loop and while loop. Therefore, I need to replace my code using If function. I have a code which has an for loop within a while loop function as shown below. How to i convert it to If function or is there other ways to not use recursive function?
while abs((max(Pnew)-min(Pnew))/max(Pnew))>0.05
for i = 1:4
f = 0.3 + (0.5-0.3)*B;
vnew(i) = vold(i)+(DPold(i) - DPbest)*f;
if DPold(i) > DPbest(i)
DPnew(i) = DPold(i) - abs(vnew(i));
else
DPnew(i) = DPold(i) + abs(vnew(i));
end
if rand(0,1)>rold(i)
DPnew(i) = DPold(i) + mean(Aold)*e;
end
D = DPnew(i);
Pnew(i) = P;
if Pnew(i) < Pold(i)
DPnew(i) = DPold(i);
Pnew(i) = Pold(i);
end
Anew(i) = 0.5*Aold(i);
rnew(i) = rold(i)*(1-exp(-0.5));
end
end
DPbest = max(DPnew);
D = DPbest;
  1 个评论
Guillaume
Guillaume 2019-7-11
编辑:Guillaume 2019-7-11
for, while, if are not functions. They are statements. A function in matlab will always start with the statement function, or starts with an @(.
for and while have nothing to do with recursive functions. Usually you would replace a recursion by a for or while loop. The code you show does not use recursion.
The purpose of for and while is repeating a task. The purpose of if is choosing alternative tasks. Completely different purpose. You can't swap one for the other.
It's really unclear what your problem is and what you want to achieve.

请先登录,再进行评论。

采纳的回答

Jon
Jon 2019-7-11
First, I think when you are saying "recursive" you mean "looping". While and For loops aren't inherently recursive. A recursive function calls itself. In any case, assuming you want to rewrite your code without loops you are "vectorizing" your MATLAB code. One of the great powers of MATLAB compared to other programming languages is its ability to operate on entire vectors or matrices without needing loops. If you want to get the benefits of using MATLAB it is important to be able to take advantage of this power. I would suggest you start by reading https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
Just a few ways you could use vectorization in your code include replacing
vnew(i) = vold(i)+(DPold(i) - DPbest)*f
with
vnew = vold + (DPold-DPbest)*f
You can also use "logical indexing" which selects just those elements of a vector, or matrix that meet a certain logical criteria, to replace for example
if Pnew(i) < Pold(i)
DPnew(i) = DPold(i);
with
idl = Pnew < Pold
DPnew(idl) = DPold(idl)
Finally, just a suggestion for future posts, it is good to use the Code button in the MATLAB Answers toolbar to include code. That way it comes out nicely formatted, and is easy to copy, so that respondents can try out your code.
  5 个评论
Bryan
Bryan 2019-7-12
Hi Guillaume,
Just want to make sure as my lecturer mentioned that while and for statements are recursive calls after he read that 'Recursive calls are not allowed in MATLAB Function blocks.' in the help section of Matlab function block. What you and Jon explained to me is new so I need to make sure that my code is actually fine. Thank you so much!
Guillaume
Guillaume 2019-7-12
No, while and for is definitively not recursion, your lecturer is wrong on that point. while and for are loop constructs. Again, recursion is a function that call itself. From the first line of the wikipedia article on recursion:
Recursion (adjective: recursive) occurs when a thing is defined in terms of itself or of its type.
It would make sense that recursion is not allowed in Simulink function blocks. It wouldn't make sense that something as basic and necessary as a for loop wouldn't be.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by