Defining piecewise constant function in matlab

239 Views Asked by At

Is there a simple way (or any inbuilt command) to define the following function in matlab:

$f(a,b,x)=\sum\limits_{i=1}^n b(i)\chi_{[a(i),a(i+1))}(x), $

where $a(i)$ and $b(i)$ are the $i^{th}$ entries of vectors $a$ and $b$ of size $n+1$ and $n$ respectively.

I understand that this function can be defined using loops. But I am looking for something simpler/one line command to define this function (see piecewise).

I don't want to define it manually using the command $piecewise,$ because size of the vectors $a$ and $b$ is quite big.

Thanks in advance

1

There are 1 best solutions below

0
On BEST ANSWER

You may try the following:(create m. files)

function f = char(a,b,x)
i=1
while i <size(a)
if (x>=a(i))&&(x<a(i+1))
f=b(i)
break
else
i=i+1
end
end
end

Hope this helps.