how do i make a function repeat 10 times.

I worte a code for takeing the avrage of pixels brightness around a pixel and change its value like so:
but now i have to make the code repeat itself on the picture 10 time
how do i do that ?

5 件のコメント

Adam
Adam 2018 年 5 月 11 日
編集済み: Adam 2018 年 5 月 11 日
Just use a for loop around the relevant part that you want to repeat. You already have a for loop in there so you clearly know how to use them.
Ahmad al-falahi
Ahmad al-falahi 2018 年 5 月 11 日
not sure i i understand. i was thinking about using a "while" loop but i dont know how to tell matlab to do it 10 times or even how matlab would know how many times the fuction has ran.
Guillaume
Guillaume 2018 年 5 月 11 日
編集済み: Guillaume 2018 年 5 月 11 日
for iteration = 1:10
%...
%code that needs to repeat 10 times
end
What is complicated about that?
Note that your code is a script, not a function.
Dennis
Dennis 2018 年 5 月 11 日
編集済み: Dennis 2018 年 5 月 11 日
I can think of 3 ways to do this.
But you need to think about what part of your code you actually want to repeat 10 times, do you want to import your image 10 times?
A)
for ii=1:10
myfunction(myinput)
end
B) A while loop would be working if you pass/return the number of executions.
numberExec=0;
while numberExec<10
numberExec = myfunction(numberExec)
end
inside your function:
function numberExec = myfunction(numberExec)
numberExec=numberExec+1;
%do stuff
end
c) Or use a recursive function:
function myfunction(numberExec)
if numberExec<10
%do stuff
myfunction(numberExec+1)
else
%plot something?
end
Ahmad al-falahi
Ahmad al-falahi 2018 年 5 月 11 日
yeah im very new to matlab :) and i put that that code you gave me before the first "for" and let it run, but the picture does not change. it should turn all grey.,

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

 採用された回答

dpb
dpb 2018 年 5 月 11 日
編集済み: dpb 2018 年 5 月 11 日

0 投票

Guillaume addressed how to loop, I'll note that the double loop above can be written succinctly as
W=filter2(ones(3)/9,p,'valid');
avoiding those two loops in their entirety. Now, what you want to do repetitively on that value is probably more easily coded without the added obfuscation of nesting loops deeper and deeper (of course, the loops are still there in the end; just the lower-level ones of little interest to the higher level code structure are hidden away where don't have to deal with them). That's the power of Matlab; might as well use the tools given.
Also NB: as you've written it, W will end up with the average in the inner portions while the outer edge will be the original value; my solution will return only the inner 'valid' values that are means.
You have to decide whether you really want that outside edge or not???

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 5 月 11 日

編集済み:

dpb
2018 年 5 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by