add value to array on each loop sequentially in matlab

To add a value to an array on each loop, you can do the following:

main.m
% Initialize array
myArray = [];

% Loop to add values to array
for i = 1:5
    myArray(end+1) = i;
end
104 chars
8 lines

In this example, the loop iterates 5 times and adds the values 1 through 5 sequentially to the array myArray. The end+1 notation appends the new value to the end of the array on each iteration.

related categories

gistlibby LogSnag