# matlab – How to assign values ​​to a vector?

## Question:

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

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

``````tot=1/2;
``````

I want the vector called 'xi' in the first position to receive the value of 'tot'

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

In the third position 'xi' receive tot+value from the second position of 'xi'

I want to do this for the 5 vector positions, just dynamically, without having to assign the 5 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)
``````

In this case, you don't want to create a vector that takes 5 values, but you want to create a vector with the 5 values ​​you want.

To use syntax similar to what you want, the important thing is to see how the `:` (Colon) operator works and work with it.

Operator `:` :

When you need a sequel, 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), it is necessary to declare the step:

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

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
``````
Scroll to Top