create a vector to store and update values in matlab

In MATLAB, you can create a vector using square brackets containing comma-separated elements:

main.m
my_vector = [1, 2, 3, 4, 5];
29 chars
2 lines

Alternatively, you can use the linspace or logspace functions to create a vector with equally spaced or logarithmically spaced values, respectively.

To access a particular element of a vector, you can use indexing with round brackets ():

main.m
second_value = my_vector(2);
29 chars
2 lines

To update a particular element of a vector, you simply assign a new value to that element:

main.m
my_vector(3) = 7;
18 chars
2 lines

You can also update multiple elements in one go using indexing and assignment:

main.m
my_vector(2:4) = [10, 20, 30];
31 chars
2 lines

Note that in MATLAB, vectors are actually a special case of arrays, where the array has only one row or column. Therefore, you can use all the same array operations on vectors, such as transposing, concatenating, and reshaping.

gistlibby LogSnag