Elimination of for loops
2 次查看(过去 30 天)
显示 更早的评论
I am learning how to optimize code in MATLAB.
I have the following mathematical experession,
where
is a unit pulse of length T,
and
is a constant.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1196228/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1196233/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1196238/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1196243/image.png)
I am looking to eliminate the for loops for n and l but I am struggling with how to apply the unit pulse.
Would welcome any input folks may have.
4 个评论
Bruno Luong
2022-11-17
编辑:Bruno Luong
2022-11-17
Then if I'm mot mistaken you don't need any loop at all. For given t
- There is at most only one l that make sp(t-lT) = 1. The inner sum has only one term that is not 0.
- The outer term is a sum of geometric series, you have a formula for it.
回答(2 个)
Bruno Luong
2022-11-17
编辑:Bruno Luong
2022-11-17
L=2;
T=1;
w0=2*pi;
N=3;
% Any t as you like without restriction
t = linspace(0,L*T,500);
l = floor(t / T);
dt = mod(t, T); % dt = t-lT
s = exp(1j*dt.*w0).*(1-exp(1j*dt*N/T))./(1-exp(1j*dt/T));
s(l < 0 | l >= N) = 0; % inner sum has 0 term for these conditions
plot(s)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!