In: Advanced Math
function[prob,flop]=Matlab(a,b)
a=[1,2,3];
b=[7,8,9];
if (length(a)~=length(b))
disp("it doesn't make sense");
end
prob=0;
flop=0;
for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);
What is wrong with my code? it only run the disp(prob) and it doesn't run the disp(flop)
here is what I got
ans =
50
However, if I take % for the function line, it shows
50
6
function dummy
clc;
clear all;
a=[1;2;3];
b=[7;8;9];
[prob,flop]=Matlab(a,b);
function[prob,flop]=Matlab(a,b)
if (length(a)~=length(b))
disp("it doesn't make sense");
end
prob=0;
flop=0;
for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);
end
end
%%%%%%%%%%%%%% Answer
50
6
>>
you should not give a=[1,2,3]; b=[7,8,9]; .. you should use by this way a=[1;2;3]; b=[7;8;9];
function dummy
clc;
clear all;
a=[1,2,3];
b=[7,8,9];
[prob,flop]=Matlab(a,b);
function [prob,flop]=Matlab(a,b)
if (length(a)~=length(b))
disp("it doesn't make sense");
end
prob=0;
flop=0;
for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);
end
end
$$$$$$$$ Note: this way also its give same answer