How do you make a function (NOT an anonymous function) from this?
"Als" means "if"
Thanks in advance

6 件のコメント

Rik
Rik 2021 年 2 月 17 日
Creating a function is fairly basic Matlab:
function output=MyFunction(input1,input2)
Is this what you mean? And why do you want to specifically avoid an anonymous function?
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
Thanks!
yeah, I need the function like you did, but I don't know how to programm this one because of the 2 variables x and k.
I don't want an anonymous function so I can use this function in other programms too.
Thanks in advance
Rik
Rik 2021 年 2 月 17 日
Well, this function signature will do the trick. Did you do a basic Matlab tutorial? I can recommend Onramp.
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
thanks, but I already know how this works, I already did it with only one variable X but know I have 2 variables X and K and I can't find a way too program this function
Walter Roberson
Walter Roberson 2021 年 2 月 17 日
k does not need to be an input for this function. The conditions have to do with x modulo 4: -2 <= mod(x,4) < 0 is the second condition, and 0 <= mod(x,4) < 2 is the first condition.

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

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 17 日

0 投票

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 件のコメント

Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
thanks!!
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
Why you do k=floor(x/4)
Rik
Rik 2021 年 2 月 17 日
Can you guess based on what the purpose is? If you divide a number by 4 and round the result down, what have you effectively done?
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
i think because in the first inequality 4k <= x? But this does not apply to k in the other inequalities, does it?
Rik
Rik 2021 年 2 月 17 日
If you would have to find the value for k with pen and paper, how would you do it?
And why would you expect two different values for k in the same expression? (in Matlab you need two separate operations for a<x<b (i.e. (a<x)&(x<b)), but mathematically that is a single expression)
Walter Roberson
Walter Roberson 2021 年 2 月 17 日
k is an element of integers.
For any given real finite x, there is exactly one integer k such that 4*k-2 <= x < 4*k+2. If you try to use any other k then both range tests will fail.
Once you know the k that makes the above true, then the two different conditions decide between halves, 4*k-2 <= x < 4*k, or 4*k <= x < 4*k+2
I coded in a slightly tricky way for the second condition: you may need some study to figure it out.
I could have coded a different way overall, finding k based on floor((x+2)/4) . If you do that, make sure you get the boundaries right.
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 17 日
oooooh thanks I get it now, really appreciate the help!

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

その他の回答 (1 件)

Asayel Alazmi
Asayel Alazmi 2021 年 2 月 21 日

0 投票

Write an mfile using for loop to output a all numbers from 1 to 4 with an increment of 0.2

1 件のコメント

Walter Roberson
Walter Roberson 2021 年 2 月 21 日
No, I don't think doing that would help solve the question that the person posted.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by