Storing the values of a for loop

That is the line to increment ze from 0.1 to 1.2 in increments of 0.1. How do I store the values of ze?
for ze=0.1:0.1:1.2 end;

3 Commenti

ze = 0.1:0.1:1.2;
can you elaborate a little?
@Aishwarya Bangalore Kumar: it creates a vector ze with values from 0.1 to 1.2 in steps of 0.1.

Accedi per commentare.

Risposte (1)

Assign ‘ze’ before the loop, then index with reference to it:
ze = 0.1:0.1:1.2;
for k = 1:numel(ze)
v(k) = % DO SOMETHING
end

8 Commenti

I still did not get the output
What are you doing in the loop?
This approach avoids defining ‘ze’ in the loop. Defining ‘ze’ before the loop allows you to use it anywhere in your code.
If you want to use elements of ‘ze’ in your calculations in the loop, just refer to them with subscripts:
ze = 0.1:0.1:1.2;
for k = 1:numel(ze)
v(k) = ze(k)^2;
end
I used to loop to increment the value of 'ze' which is used in an equation.
The loop here increments ‘ze’ with every iteration of the ‘k’ loop.
I want my variable 'ze' to increase from 0.1 to 1.2 by 0.1times, this is the problem and I thought I will use a loop to do it. That is the only function it has to perform.
You do not need a loop to create or increment ‘ze’. This code does it automatically:
ze = 0.1:0.1:1.2;
Also, the colon (link) operator creates ‘ze’ in a way that minimises the errors that occur in floating-point calculations. A loop that begins from 0.1 and increments by 0.1 to 1.2 would propagate, rather than minimise, those errors.
Thank you I understood now , it was a big help!
As always, my pleasure!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by