![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/293628/image.png)
Replacing for loop with matrix math to increase computational efficiency.
1 次查看(过去 30 天)
显示 更早的评论
I am using the following code to compute the Sx, Su, Sw and Sd matrices. But I have found out them to be very computationally expensive and also I understand there is a better way to do this. Can anyone please suggest how these computations can be done more efficiently? I am also considering converting these matrices to sparce matrices after assignment for the further calculations.
N = pred_horizon/Ts; % N = 500
Sx = zeros(8*(N+1),8);
Su = zeros(8*(N+1),8*(N));
Sw = zeros(8*(N+1),8*(N));
Sd = zeros(8*(N+1),1);
for i=1:(length(Sx)-7)
Sx(i:i+7,:) = power(Ad,(i-1));
end
for i=2:size(Su,1)-7
for j=1:min((i-1),size(Su,2)-7)
Su(i:i+7,j:j+7) = power(Ad,(i-j-1))*Bd;
end
end
for i=2:size(Sw,1)-7
for j=1:min((i-1),size(Sw,2)-7)
Su(i:i+7,j:j+7) = power(Ad,(i-j-1))*Ed;
end
end
for i=2:length(Sd)-7
Sd(i:i+7,:) = power(Ad,i-2)*Dd;
end
0 个评论
回答(1 个)
darova
2020-5-17
You can increase for loop step
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/293628/image.png)
You are overwriting same values all the time. THere is no need of it
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!