How to assign values to a vector?

Asked

Viewed 678 times

3

I want to create an array in Matlab that takes 5 values from the 'tot' variable'.

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

tot=1/2;

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

In second position 'xi' get tot+value from first position 'xi'

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

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

FOLLOWS BELOW THE CODE THAT IS NOT RIGHT:

tot=1/2;

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

disp(xi)
  • 1

    Please do not use high box in titles.

1 answer

0

In this case, you do not want to create an array that takes 5 values, but rather create an array with the 5 desired values.

To use syntax similar to what you want, the important thing is to see how the 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 must 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.