Write a Matlab program using FOR loop that computes the sum

Write a Matlab program using FOR loop that computes the sum of the elements of an array. Load the lab8.mat file and from it, use Array1 as the sample array to compute the sum. Use the length command to know the elements in the array. The code I have tried is

clear clc; load lab8 x = Array! ; 3um=0; for k = 0: length (x) sum = sum + x (k) ; end string = [ \'The summation is \' num2str (sum)]; disp (string)

Solution

In matlab the index of 1st element of an array is 1;

so the loop variable k must be 1 to length(array);

so the modified statement s is for k=1:length(x)

Complete program below

clear;
clc;
load lab8;
x=Array1;
sum=0;
for k=1:length(x)
sum=sum+x(k);
end
string=[\'The summation is\' num2str(sum)];
disp(string);

Write a Matlab program using FOR loop that computes the sum of the elements of an array. Load the lab8.mat file and from it, use Array1 as the sample array to c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site