Optimizing or Simplifying code
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Jan
on 1 May 2016
Edited: Jan
on 1 May 2016
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?
0 Comments
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!