How to include a conditional function inside a "for k = 1 : 200" loop?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to contain a function inside a loop as mentioned. The loop is "for k = 1 : ite" where ite=200. The function I am calling contains conditional statements such as "if k == 1," and "if k > 1,". It is returning an error saying that k is undefined when running the conditional statement of the function. Do I need to load the k variable from the original code inside the function?
1 个评论
Azzi Abdelmalek
2013-7-30
Do I need to load the k variable from the original code inside the function?
Can you explain ?
采纳的回答
Evan
2013-7-30
编辑:Evan
2013-7-30
Yes, you need to pass the k value, and any other values used from the calling function, into your function that you call within the loop. Here's a simple example:
function my_function
a = rand(1,100);
ite = 100;
for k = 1:ite
m(k) = process(k);
end
end
function x = process(idx)
x = a(idx) > 0.5;
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!