![](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!