any ideas for making code faster?
2 次查看(过去 30 天)
显示 更早的评论
clc;
clear all;
R = [0.004 0.0057 0.005 0.005 0.0045 0.0044 0.005];
X = [0.05 0.0714 0.0563 0.045 0.0409 0.05 0.05];
L = [100 70 80 100 110 90 100]; %KM
for a = 1:7
Z1(1,a) = ((R(1,a) + i*(X(1,a))))*L(1,a);
end
z1 = abs(Z1); %begining of line
z2 = abs(Z1); %end of line
z = [z1 z2]; %Z tottal
Z2 = Z1;
Z = [Z1 Z2];
ctr1 = [240 240 160 240 240 240 160];
ctr2 = [240 160 240 240 240 240 160];
ctr = [ctr1 ctr2];
ptr = (150*1000)/110;
tetta1 = angle(Z1);%begining of line
tetta2 = angle(Z1);%end of line
tetta = [tetta1 tetta2];
z_final = z * 0.8;
for i= 1:14
ZZ1(1,i) = ((z_final(1,i))/(cos(tetta(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
L2 = [14 2 50 35; 3 0 40 0; 4 0 50 0;5 0 55 0; 7 6 50 45; 1 0 50 0; 2 8 35 50; 13 0 45 0; 14 8 50 50; 9 0 35 0; 10 0 40 0; 11 0 50 0; 7 12 50 55; 6 12 45 55]; %
% backup1 backup2 distance1 distance2
A = zeros (14,14);
for i = 1:14
A(i,L2(i,1)) = Z(1,L2(i,1))/2;
if L2(i,2) ~= 0
A(i,L2(i,2)) = Z(1,L2(i,1))/2;
else
end
end
for i = 1:14
if L2(i,2) == 0
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) <= L2(i,4);
z2new(1,i) = A(i,L2(i,1));
else
L2(i,3) >= L2(i,4);z2new(1,i) = A(i,L2(i,2));
end
end
ZZ2_2 = Z + z2new;
z2_final = abs(ZZ2_2);
tetta3 = angle(ZZ2_2);
for i= 1:14
ZZ2(1,i) = ((z2_final(1,i))/(cos(tetta3(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
Z22new = z2new * 2;
L3 = [3 80; 4 100; 5 110; 6 90; 1 100; 2 70; 2 70; 7 100; 13 90; 8 100; 9 70; 10 80; 2 70; 1 100];
for i = 1:14
z3new(1,i) = (Z(1,L3(i,1)))*(2/5);
end
ZZ_3 = (Z + Z22new) + z3new;
Z3_final = abs(ZZ_3);
tetta4 = angle(ZZ_3);
for i= 1:14
ZZ3(1,i) = ((Z3_final(1,i))/(cos(tetta4(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
for i=1:14
cti2(1,i) = 0.3;
cti3(1,i) = 0.6;
end
backup = [6 1; 13 7; 7 2; 14 6; 8 13; 5 6; 9 14; 4 5;
10 9; 3 4; 2 3; 11 10; 1 2; 12 11]; % column2==0>>one relay backub
backup1 = [13 12; 7 8; 14 12; 5 7; 9 8; 1 14];
for i = 1:14
if ZZ2(1,backup(i,1)) > ZZ2(1,backup(i,2))
cti2(1,backup(i,1)) = cti2(1,backup(i,1)) + 0.3;
end
if ZZ3(1,backup(i,1)) > ZZ3(1,backup(i,2))
cti3(1,backup(i,1)) = cti3(1,backup(i,1)) + 0.3;
end
end
for i = 1:6
if cti2(1,backup1(i,1)) == 0.3
if ZZ2(1,backup1(i,1)) > ZZ2(1,backup1(i,2))
cti2(1,backup1(i,1)) = cti2(1,backup1(i,1)) + 0.3;
end
end
if cti3(1,backup1(i,1)) == 0.6
if ZZ3(1,backup1(i,1)) > ZZ3(1,backup1(i,2))
cti3(1,backup1(i,1)) = cti3(1,backup1(i,1)) + 0.3;
end
end
end
How Can i make my code faster and shorter?any ideas ?time of execution is really important to me
5 个评论
Dyuman Joshi
2022-6-25
Example -
%Replace this
for a = 1:7
Z1(1,a) = ((R(1,a) + i*(X(1,a))))*L(1,a);
end
%with
Z1=(R+i*X).*L
DGM
2022-6-25
This looks like a bug.
for i = 1:14
if L2(i,2) == 0
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) <= L2(i,4);
z2new(1,i) = A(i,L2(i,1));
else % is this supposed to be an elseif statement?
L2(i,3) >= L2(i,4); % this test does nothing
z2new(1,i) = A(i,L2(i,2));
end
end
What's the intended behavior?
采纳的回答
DGM
2022-6-25
编辑:DGM
2022-6-25
Here. This is about 5x as fast, though it could be improved I'm sure.
R = [0.004 0.0057 0.005 0.005 0.0045 0.0044 0.005];
X = [0.05 0.0714 0.0563 0.045 0.0409 0.05 0.05];
L = [100 70 80 100 110 90 100]; %KM
Z1 = ((R + 1i*X)).*L;
ctr = [240 240 160 240 240 240 160];
ptr = (150*1000)/110;
z_final = abs(Z1) * 0.8;
tetta = angle(Z1);
ZZ1 = z_final./cos(tetta - (pi/4)) .* ctr/ptr;
ZZ1 = repmat(ZZ1,[1 2]);
Z = repmat(Z1,[1 2]);
ctr = repmat(ctr,[1 2]);
% backup1 backup2 distance1 distance2
L2 = [14 2 50 35; 3 0 40 0; 4 0 50 0;5 0 55 0; 7 6 50 45; 1 0 50 0; 2 8 35 50; ...
13 0 45 0; 14 8 50 50; 9 0 35 0; 10 0 40 0; 11 0 50 0; 7 12 50 55; 6 12 45 55];
linidx = 1:14; % useful for indexing ops
A = zeros(14,14);
idx = sub2ind([14 14],linidx,L2(:,1).'); % bk1 indexing
A(idx) = Z(L2(:,1))/2;
mk = L2(:,2) ~= 0;
idx = sub2ind([14 14],linidx(mk),L2(mk,2).'); % bk2 indexing
A(idx) = Z(L2(linidx(mk),1))/2;
% i'm sure this loop can be eliminated,
% but i'm not going to bother with testing that
z2new = zeros(1,14);
for i = 1:14
if L2(i,2) == 0
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) <= L2(i,4)
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) >= L2(i,4) % i assume the original code was wrong
z2new(1,i) = A(i,L2(i,2));
end
end
ZZ2_2 = Z + z2new;
ZZ2 = abs(ZZ2_2)./cos(angle(ZZ2_2) - (pi/4)) .* ctr/ptr;
L3 = [3 80; 4 100; 5 110; 6 90; 1 100; 2 70; 2 70; 7 100; 13 90; 8 100; 9 70; 10 80; 2 70; 1 100];
z3new = Z(1,L3(:,1))*(2/5);
ZZ_3 = Z + 2*z2new + z3new;
ZZ3 = abs(ZZ_3)./cos(angle(ZZ_3) - (pi/4)) .* ctr/ptr;
cti2 = repmat(0.3,[1 14]);
cti3 = repmat(0.6,[1 14]);
backup = [6 1; 13 7; 7 2; 14 6; 8 13; 5 6; 9 14; 4 5;
10 9; 3 4; 2 3; 11 10; 1 2; 12 11]; % column2==0>>one relay backub
backup1 = [13 12; 7 8; 14 12; 5 7; 9 8; 1 14];
% i'm not going to bother with these either. i don't have any time left
for i = 1:14
if ZZ2(1,backup(i,1)) > ZZ2(1,backup(i,2))
cti2(1,backup(i,1)) = cti2(1,backup(i,1)) + 0.3;
end
if ZZ3(1,backup(i,1)) > ZZ3(1,backup(i,2))
cti3(1,backup(i,1)) = cti3(1,backup(i,1)) + 0.3;
end
end
for i = 1:6
if cti2(1,backup1(i,1)) == 0.3
if ZZ2(1,backup1(i,1)) > ZZ2(1,backup1(i,2))
cti2(1,backup1(i,1)) = cti2(1,backup1(i,1)) + 0.3;
end
end
if cti3(1,backup1(i,1)) == 0.6
if ZZ3(1,backup1(i,1)) > ZZ3(1,backup1(i,2))
cti3(1,backup1(i,1)) = cti3(1,backup1(i,1)) + 0.3;
end
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!