basic vectors in matlab

To create a basic vector in MATLAB, you can use the following syntax:

main.m
vec = [1 2 3 4 5];
19 chars
2 lines

This creates a row vector with the values 1 through 5. You can also create a column vector by transposing a row vector like so:

main.m
vec = [1 2 3 4 5]';
20 chars
2 lines

You can perform basic arithmetic on vectors using element-wise operations. For example, you can add two vectors like so:

main.m
vec1 = [1 2 3];
vec2 = [4 5 6];
result = vec1 + vec2;
54 chars
4 lines

This results in a vector result with values [5 7 9].

You can also access individual elements of a vector using indexing. For example, to access the second element of vec, you can do:

main.m
second_element = vec(2);
25 chars
2 lines

This sets second_element to the value 2.

Finally, you can use MATLAB's built-in vector functions to perform operations like summing or finding the maximum value of a vector. Some common vector functions include sum, max, and min. For example:

main.m
vec = [1 2 3 4 5];
vec_sum = sum(vec);
vec_max = max(vec);
vec_min = min(vec);
79 chars
5 lines

This sets vec_sum to 15, vec_max to 5, and vec_min to 1.

related categories

gistlibby LogSnag