Step function with variable number of steps

3 vues (au cours des 30 derniers jours)
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 1 Juin 2022
Hello friends,
I have a simple question but would like to find an efficient way to implement it for my very heavy codes. Assume that we wish define a picewise function f over a grid called mesh. All the pieces are constant. So, f(x) = c1 over [mesh(1) mesh(2)), f(x) = c2 over [mesh(2) mesh(3)), ..., f(x) = c_n over [mesh(n) mesh(n+1)]. The problem is that I do not know the number of pieces beforehand. So, please do not give me an answer based on 2 pieces or 3, etc. I know how to handle such problems.
Furthermore, we can easily solve the pproblem I pposed using a for-loop. I am not interested in this solution as this is very time-conssuming.
Thanks in advance,
Babak

Réponse acceptée

Matt J
Matt J le 1 Juin 2022
f=@(x) interp1(mesh,c,x,'previous');
  1 commentaire
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 1 Juin 2022
This is exactly what I wanted. Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by