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

Please i need hint on how to manipulate the output from a for loop do summation of differences between it and a data set. An example is shown below. I tried it but it only computed for the first for loop 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)

 Respuesta aceptada

Mischa Kim
Mischa Kim el 15 de Abr. de 2014
Editada: Mischa Kim el 15 de Abr. de 2014
Segun, are you trying to compute the cumulative sum?
A = 0;
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 = A + sum(Y)*Z;
end
disp(A)

3 comentarios

Thanks for your help,I tried your method but I found out that the computer could did not compute correctly as I think it could pick M and N i.e 4.5-(output1) from N which is 2.Please is there a way to make the computer pick a data set one by one and execute it?
Could you be more specific? What are your inputs and what is the expected output?
Thanks for your help.What I mean is that I run the above program using matlab I got an answer of 40.25 .To crosscheck whether it is correct I did the manipulation manually I got 48.89.This shows something is wrong somewhere.

Iniciar sesión para comentar.

Más respuestas (1)

Deepana
Deepana el 15 de Abr. de 2014
Editada: Deepana el 15 de Abr. de 2014
Can you tel more about what exactly your looking for?

1 comentario

Thanks the one that is giving problem is the use of "goto" statement in matlab to make the computer go to a particular line and repeat the whole process down if a condition is not met. Just an example not the complete program as it is long. A= 0; 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 = A + sum(Y)*Z; end if A<1; go to line (1) elseif A>1; end disp(A)

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Abr. de 2014

Comentada:

el 15 de Abr. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by