How to construct indicator function in array function?

조회 수: 28 (최근 30일)
WeiHung Liao
WeiHung Liao 2021년 3월 31일
댓글: WeiHung Liao 2021년 4월 3일
In my original problem, I want to construct piecewise linear equation to analyze its wavefront.
However, the number of points are about 200~400. Thus, using "pw" to construct function is not efficient to my problem and it works case by case.
Hence, I try to construct a indicator function as follows. For example,
f= [2;3;4];
g= [2.5;4;8];
h= arrayfun(fg(x,f,g),f,g)
function out=fg(x,f,g)
fval=f;
gval=g;
if x>=fval & x<=gval
out=1;
else
out=0;
end
end
Its result show "Unrecognized function or variable 'x'."
I also refer to the related question Passing array arguments to an anonymous function, but I still cannot work out.
I WANT: it can show like this
h(2.1)= [1; 0; 0] , h(4)=[0; 1; 0]

채택된 답변

Stephen23
Stephen23 2021년 3월 31일
I don't see why you need arrayfun:
f = [2;3;4];
g = [2.5;4;8];
fun = @(x) x>=f & x<=g;
fun(2.1)
ans = 3×1 logical array
1 0 0
fun(4) % your example is incorrect: 4 is definitely equal to 4 and less than 8
ans = 3×1 logical array
0 1 1
  댓글 수: 2
WeiHung Liao
WeiHung Liao 2021년 4월 3일
In the beginning,I define f and g as anonymous functions and h as mentioned above. But I run h(.),it return it is 1x1 cell. Thus,I refer the question listed above and try arrayfun to realize it. My original goal is to defined piecewise continuous function with given breakpoints.
WeiHung Liao
WeiHung Liao 2021년 4월 3일
Thank for your answer, it helps me to realize my construction of piecewise linear function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by