sum of 10 numbers, each time the result must store in a array in matlab

To compute the sum of 10 numbers and store the result in an array in MATLAB, you can follow these steps:

  1. Initialize an array to store the results.
  2. Use a loop to iterate through the numbers and calculate the sum.
  3. Store the sum in the array for each iteration.

Here's an example code:

main.m
% Initialize array to store the results
results = zeros(1, 10);

% Generate 10 random numbers between 1 and 100
numbers = randi([1, 100], 1, 10);

% Compute the sum and store it in the array
for i = 1:10
    results(i) = sum(numbers(1:i));
end

% Display the resulting array
disp(results);
290 chars
14 lines

In this example, we generate 10 random numbers between 1 and 100. Then, we iterate through each number and compute the cumulative sum, storing the result in the results array. Finally, we display the resulting array.

Note that you can replace the numbers array with any other array of 10 numbers according to your requirements.

related categories

gistlibby LogSnag