Hello Experts;
I am new in matlab and trying to learn how we can create function? For Example . I have this code how I can create function file out of it? It will be easy for me to learn from this code.
g=imread('cameraman.tiff');
original=g;
[m,n]=size(g);
subplot(2,2,1)
imshow(g,[])
[m,n]=size(g);
a = 3;
b = 3;
num_iter = 5;
for k=1:num_iter
for i=1:m
for j=1:n
r = mod([round(abs(1-(a*(i^4))+j)),round(abs(b*i))],[m n]);
x(i,j)=g(r(3)+1,r(4)+1);
end
end
g=x;
end
subplot(2,2,2)
imshow(x);

 採用された回答

Star Strider
Star Strider 2020 年 9 月 22 日

0 投票

See the documentation on Function Basics.

9 件のコメント

marie lasz
marie lasz 2020 年 9 月 22 日
thanks but I am confused and would like to learn from the code given above
Star Strider
Star Strider 2020 年 9 月 22 日
Experiment. The world will not end if your code throws errors. MATLAB is reasonably robust.
Figure out what arguments (inputs) the function needs, and what outputs it should return to the workspace that called it. Then, write the function to do that.
Note that the function does not necessarily need arguments or outputs, since how you write it depends on what you want it to do.
marie lasz
marie lasz 2020 年 9 月 22 日
編集済み: marie lasz 2020 年 9 月 22 日
what do you mean by your indirect phrases 'The world will not end if your code throws errors.'?? And I didn't ask you about code. If you don't want to answer then don't throw these phrases , no one is forcing you to answer. Thanks
Star Strider
Star Strider 2020 年 9 月 22 日
I mean that I am encouraging you to experiment! The worst possible outcome is that your code throws an error. That is frustrating (and occasionally embarrassing), however when I experiment, I always learn from my mistakes, and generally do not repeat them afterwards.
There is an extremely remote possibility that your code could crash MATLAB or crash your computer, however that is unlikely. MATLAB has become much more robust over the years, and catches most such programming errors either when they are written (if you use the MATLAB Editor), or when the code executes.
marie lasz
marie lasz 2020 年 9 月 22 日
Yes I tries and trying to learn. I never relies on help but I appreciate if someone tries to teach me. Anyways thanks .
Star Strider
Star Strider 2020 年 9 月 22 日
That looks to be a good start.
Save it as: shuf.m somewhere on your MATLAB user path (if you have not already done so), then call it from a script to see if you get the result you expect.
Remember to call it as:
x = shuf(g,num_iter);
with the appropriate agrguments. The ‘x’ output will then be in your workspace.
Steven Lord
Steven Lord 2020 年 9 月 22 日
In addition to what Star Strider has said, if you're not sure if your code is behaving correctly I recommend stepping through it line by line, checking that each line does what you expect it to do. The debugging tools in MATLAB will let you do this. Set a breakpoint on the first line, run your code, and step through the file.
marie lasz
marie lasz 2020 年 9 月 22 日
thanks for your suggestions and finally, I did it successfully and learned :-)
Star Strider
Star Strider 2020 年 9 月 22 日
Our pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (1 件)

hatem
hatem 2023 年 12 月 27 日

0 投票

>> x =imread('cameraman.tif');
>> y = x*0;
>> [w h]= size(x);
>> for i = 1:w
for j =1:h
b =bitget(x(i,j),6)
y(i,j)=bitset(y(i,j),6,b);

カテゴリ

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

質問済み:

2020 年 9 月 22 日

回答済み:

2023 年 12 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by