How to assign values to a vector?

3

I want to create a vector in matlab that gets 5 values, from the variable 'tot'.

In my program I have a 'tot' variable that gets 1/2

tot=1/2;

I want the vector named 'xi' in the first position to get the value of 'tot'

In the second position 'xi' receives tot + value from the first position of 'xi'

In the third position 'xi' receives tot + value of the second position of 'xi'

I want to do this for the 5 vector positions, only dynamically, without having to assign the five vector values one by one.

FOLLOW THE CODE THAT IS NOT RIGHT:

tot=1/2;

xi=(tot:tot)// a partir daqui não sei mais fazer

disp(xi)
    
asked by anonymous 05.11.2017 / 01:01

1 answer

0

In this case, you do not want to create a vector that receives 5 values, but create a vector with the 5 desired values.

To use syntax similar to what you want, what's important is to see operator : (Colon) works and works with it.

Operator : :

When you need a sequence, it works two ways

a=1:3; %passo default (=1), saída > a=[1 2 3]  
a=1:2:3; %valor do passo selecionado , saída > a=[1 3]

If you need a negative step (even if = 1), you need to declare the step:

a=3:-1:1; %valor do passo selecionado , saída > a=[3 2 1]

In your case:

You have two simple options using : :

tot=1/2;

xi=(1:5)*tot;

% OU

xi=tot:tot:(tot*5);

%ambos tem a mesma saída:
xi=
   0.5000    1.0000    1.5000    2.0000    2.5000
    
11.12.2017 / 18:37