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

回答(1 个)

darova
darova 2020-5-17
You can increase for loop step
You are overwriting same values all the time. THere is no need of it

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by