Defining a piecewise function using a list

조회 수: 4 (최근 30일)
Noureddine Toumi
Noureddine Toumi 2018년 9월 12일
답변: Walter Roberson 2018년 9월 12일
I would like to define a piecewise function by providing two lists and using a for loop eg:
xlist = {1, 2, 3, 4}
ylist = {4, 5, 6}
f(x):= { ylist[1] if x in [ xlist[1],xlist[2] ]; ylist[2] if x in [ xlist[2],xlist[3] ]; ylist[3] if x in [ xlist[3],xlist[4] ]; 0 otherwise}

답변 (1개)

Walter Roberson
Walter Roberson 2018년 9월 12일
t = num2cell(piecewise([reshape([x >= xlist(1:end-1) & x < xlist(2:end)], [], 1), ylist(:); sym('true'), 0].'));
f = piecewise(t{:});
I had to make assumptions about whether you wanted your ranges to be semi-open or semi-closed

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by