how to manipulate the output of a for loop to get the sum of the output data set

Status
Not open for further replies.

shegmite

Member level 1
Joined
Apr 8, 2012
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,496
Please i need hint on how to manipulate the output from a for loop do summation of differences between it and a data set one at a time. An example is shown below. I tried it but it only computed for the first for loop data neglecting the other data set.
for f = [1 4 8 10];
b=2;
c=1;
d=1;
n=4;
N= f*b*c*d
M=[4.5 10 20 25];
Y = (M-N)/M;
Z=(100/n);
A =(sum(Y)*Z);
end
disp(A)
 

Hi shegmite,

you need to store the result of each iteration in a different location.
A must be a vector.
Like this:

A=zeros(1,4);
ii=1;
for f = [1 4 8 10];
b=2;
c=1;
d=1;
n=4;
N= f*b*c*d
M=[4.5 10 20 25];
Y = (M-N)/M;
Z=(100/n);
A(ii)=(sum(Y)*Z);
ii=ii+1;
end
disp(A)

Regards

Z
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…