Changing amount of decimals in between loops

1 Ansicht (letzte 30 Tage)
Caroline Prekker
Caroline Prekker am 2 Jul. 2020
Bearbeitet: Walter Roberson am 3 Jul. 2020
So I have a for loop that repeats 3 times, the first time my answer needs to have 3 decimal places but then when it runs the second and third time I need to have 4 decimal places. How do I change the number of decimal places between the three loops
  1 Kommentar
Walter Roberson
Walter Roberson am 2 Jul. 2020
Bearbeitet: Walter Roberson am 3 Jul. 2020
Places_to_round_to = [3 4 4];
for K = 1 : whatever
calculate something
YourVariable = round(YourVariable, Places_to_round_to(K));
end

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Les Beckham
Les Beckham am 3 Jul. 2020
Just index into the vector that you created that specifies the number of places to which you wish to round for that iteration in the loop.
YourVariable = round(YourVariable, Places_to_round_to(K));

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by