Optimizing or Simplifying code

2 ビュー (過去 30 日間)
Zaakir  Essop
Zaakir Essop 2016 年 5 月 1 日
編集済み: Walter Roberson 2016 年 5 月 2 日
Hi
I have the following code:
for count = 1:1:size(PointLoads,1);
if Positions > PointLoads(count,2) % Checks if the location of PointLoad falls within posiyions
ShearForce = 0;
else
Z = (Positions<=PointLoads(count,2)); %If condition is false then Positions are multiplied by the corresponding forces
ShearForce = ShearForce + PointLoads(count,1).*Z;
end
end
%ShearForce for DistributedLoadsLoads
for count = 1:1:size(DistributedLoads,1)
if Positions > max(DistributedLoads(count,2:3))
ShearForce = ShearForce;
else
DistributedLoads1 = DistributedLoads(count,1);
A = (max(DistributedLoads(count,2:3)) - min(DistributedLoads(count,2:3)));
B = (Positions<min(DistributedLoads(count,2:3)));
C = (max(DistributedLoads(count,2:3)) - Positions);
D = (and(Positions>=min(DistributedLoads(count,2:3)),Positions<=max(DistributedLoads(count,2:3))));
ShearForce = ShearForce + DistributedLoads1.*A.*B;
ShearForce = ShearForce + DistributedLoads1.*C.*D;
end
end
I was wondering if anyone would be able to assist me in optimizing the code and perhaps making it less complicated.
Thanks

回答 (1 件)

Jan
Jan 2016 年 5 月 1 日
編集済み: Jan 2016 年 5 月 1 日
index = (Positions <= PointLoads(k, 2));
ShearForce = sum(PointLoads(index, 1));
%ShearForce for DistributedLoadsLoads
D = DistributedLoads; % Nicer name
maxD = max(D(:, 2:3), [], 1);
minD = min(D(:, 2:3), [], 1);
for k = 1:size(D,1)
if Positions >= maxD(k)
A = maxD(k) - minD(k);
B = (Positions < minD(k));
C = maxD(k) - Positions;
D = (and(~B, Positions <= maxD(k)));
ShearForce = ShearForce + D(k,1) .* (A .* B + C .* D);
end
end
Is "Positions" a vector? If so, are you sure that "if Positions >= maxD(k)" does, what you want?

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by