How do I define piecewise constant function in for loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Saurabh Madankar
el 28 de Oct. de 2023
Editada: Dyuman Joshi
el 28 de Oct. de 2023
For example, say I have data points given by
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523551/image.png)
and corresponding output points
. Now I want to define a piecewise constant function y such that on
its
,
on
,
on
,
on
and so on.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523556/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523561/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523566/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523571/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523576/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523581/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523586/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523591/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1523596/image.png)
0 comentarios
Respuesta aceptada
Torsten
el 28 de Oct. de 2023
Editada: Torsten
el 28 de Oct. de 2023
Give the correct values to the arrays "left_limit_of_ith_interval" and "right_limit_of_ith_interval" in the following code:
left_limit_of_ith_interval = ...;
right_limit_of_ith_interval = ...;
fun = @(T)0;
for i = 1:M-1
fun = @(T) fun(T) + x(t(i)).*(T>=left_limit_of_ith_interval(i)).*(T<right_limit_of_ith_interval(i));
end
1 comentario
Dyuman Joshi
el 28 de Oct. de 2023
Editada: Dyuman Joshi
el 28 de Oct. de 2023
How about this?
M=5;
t = ((1:M)-1/2)/M;
t = [0 t+t(1)]
fun = @(T) 0;
for i = 2:M
fun = @(T) fun(T) + x(t(i)).*(T>=t(i-1)).*(T<t(i));
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!