Is there an efficient way to write a piecewise function ?
이전 댓글 표시
Hi,
I have a very long 1 x 4000 temperature vector, A = [a1 a2 a3 a4 a5 a6 a7 .... a4000].
I also have the hours vector T = [t1 t2 t3 t4 t5 .....t4000]. A and T are given.
Now, I wish to have a stair function, such that,
if 0<=t<t1 func = a1;
if t1<=t<t2 func = a2;
if t2<=t<t3 func = a3;...
In the stuff I am doing, I used to use "piecewise" function for small number of A and T for testing (maybe just a1 to a10). So, I can manually wrote pieces one by one. Now, I need to use the real stuff A,which is very long. I cant do this one by one anymore. So, my question is, is there a way I could make a piecewise stair function for given A and T.
Please do notice that:
- This stair function will be feed into integration later. It must be ONE function, that's why I was using "piecewise" (It works well). In other words, I do NOT use it just for plot.
- Honestly, I dont know this piecewise function will allow me to manually write that many pieces to a4000 or not.
채택된 답변
추가 답변 (1개)
Bruno Luong
2021년 8월 24일
tempfun = @(x) interp1([0 T], A([1:end end]), x, 'previous');
temp = tempfun(whatever_query_t);
카테고리
도움말 센터 및 File Exchange에서 Elementary Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
