MATLAB Answers

Functions with 2 variables

1 view (last 30 days)
How do you make a function (NOT an anonymous function) from this?
"Als" means "if"
Thanks in advance
  6 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2021
format long g
mat2str(g(-9:.5:9))
ans = '[0 0.5 2 3.5 4 3.5 2 0.5 0 0.5 2 3.5 4 3.5 2 0.5 0 0.5 2 3.5 4 3.5 2 0.5 0 0.5 2 3.5 4 3.5 2 0.5 0 0.5 2 3.5 4]'
function output = g(x)
k = floor(x/4);
rm = x - 4*k;
mask = rm < 2;
output = zeros(size(x));
output(mask) = 4-2*(rm(mask) - 1).^2;
output(~mask) = 2*(rm(~mask) - 3).^2;
end
  7 Comments
Jens Petit-jean
Jens Petit-jean on 17 Feb 2021
oooooh thanks I get it now, really appreciate the help!

Sign in to comment.

More Answers (1)

Asayel Alazmi
Asayel Alazmi on 21 Feb 2021
Write an mfile using for loop to output a all numbers from 1 to 4 with an increment of 0.2
  1 Comment
Walter Roberson
Walter Roberson on 21 Feb 2021
No, I don't think doing that would help solve the question that the person posted.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!