Functions with 2 variables
조회 수: 2 (최근 30일)
이전 댓글 표시
How do you make a function (NOT an anonymous function) from this?
"Als" means "if"
Thanks in advance
댓글 수: 6
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
2021년 2월 19일
채택된 답변
Walter Roberson
2021년 2월 17일
format long g
mat2str(g(-9:.5:9))
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
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.
추가 답변 (1개)
Asayel Alazmi
2021년 2월 21일
Write an mfile using for loop to output a all numbers from 1 to 4 with an increment of 0.2
댓글 수: 1
Walter Roberson
2021년 2월 21일
No, I don't think doing that would help solve the question that the person posted.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!