I want to create a function z=f(a,b)..how can I create like this using the below code?
1 view (last 30 days)
Show older comments
PLACEIUS NISHIGA G
on 20 Jan 2018
Commented: Walter Roberson
on 20 Jan 2018
if true
a=Ns;
b=8;
if ~mod(a,b)
z=0
else
z=b-mod(a,b);
end
end
0 Comments
Accepted Answer
Image Analyst
on 20 Jan 2018
MATLAB documentation tells you how to make functions. Like, to make your "f" function you'd do this:
function z = f(a, b)
if ~mod(a,b)
z=0
else
z=b-mod(a,b);
end
To call it, you'd do this:
a = Ns;
b = 8;
z = f(a, b)
4 Comments
Walter Roberson
on 20 Jan 2018
If you are using R2016a or earlier, you will need to store the code starting from 'function' in the file f.m
More Answers (1)
Walter Roberson
on 20 Jan 2018
The calculation simplifies.
f = @(a,b) mod(-a, b);
This applies even for negative a
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!