Optimizing or Simplifying code

조회 수: 1 (최근 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 CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by