using nested for loop to create new vector in matlab

To create a new vector using nested for loops in Matlab, you can follow the syntax below:

main.m
%initialize the size and values of the vector you want to create
vector_size = 10;
new_vector = zeros(vector_size, 1);

%nested for loop to assign values to each element in the vector
for i = 1:vector_size
    for j = 1:i
        new_vector(i) = new_vector(i) + j;
    end
end
277 chars
11 lines

In this example, we create a vector of size 10 and set its initial value to 0 using the zeros() function. Then we use two nested for loops to iterate through each element of the vector and assign the sum of the inner loop index to that element.

Note that in the inner loop, we use the current outer loop index i as the upper bound to sum only the elements up to the current index.

gistlibby LogSnag