Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How Do I To Store First Value To Satisfy Condition In A For Loop?

1 Ansicht (letzte 30 Tage)
Matome Moremi
Matome Moremi am 31 Mär. 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I have a code that has to calculate the amount of ballast needed to be added to move the center of mass to the center geometry and where it should be added(Its position)
I made a for loop with the added mass as the index. This gives multiple Positions but i need to get the FIRST to satisfy my required conditions
My code is as follows...
I want the first Positive Pos_add that is Less or equal to DesiredCoM. Instead of all the Pos_adds that meet this condition...(which it gives me)
for M_add=1:0.01:Mass;
Pos_add=(-sum(Masses.*Positions)+(DesiredCoM)*(Mass+M_add))/M_add
if Pos_add>0 & Pos_add<=DesiredCoM
break
end
endfor

Antworten (1)

William Smith
William Smith am 3 Apr. 2018
% Find the position of the first element that matches.
pos = find(Pos_add > 0 && Pos_add <= DesiredCoM, 1, 'first');
% Get the value
answer = Pos_add(pos);

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by