how to describe this matlab function?

6 views (last 30 days)
tariq khaldi
tariq khaldi on 28 Aug 2020
Answered: Bjorn Gustavsson on 28 Aug 2020
Hi guys,
I need your help to describe the following matlab code in a scientific way:
function [y,x]= select1()
x=10*rand(1,7);
m=[3 7];
dif1=abs(m(1)-x);
dif2=abs(m(2)-x);
if (sum(dif1(:)<dif2(:)) > sum(dif1(:)>dif2(:)));
y= m(1);
else
y= m(2);
end
I understand that this function selects m1 if most of x values are closer to m1 than to m2, and selects m2 else, but I am having a hard time describing its steps properly.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 28 Aug 2020
At the commandline prompt type this:
dbstop in select1
Then call the function from the commandline:
select1
That will open the m-file in the editor, and you can execute the function line-by-line (pushing the green "step"-button), and after each step you can inspect the values of all variables (and plot them with the different (use the 1-D, 2-D plotting functions as you see fit). Then you can figure out what's happening. You will learn at least 2 skills: initial debugging, and algorithm-interpretation.
HTH

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!