Borrar filtros
Borrar filtros

To subtract the output of a for loop manipulation sequentially from a data set

2 visualizaciones (últimos 30 días)
Please i need hint on how to make the output of this for loop (N) to be subtract from M sequentially without skipping any of the numbers. I tried this code and it did not give me the correct answer as it ignored some numbers.
for f = [1 4 8 10 14 18 22 26 30 34 38 42 45 48 52];
b=2;
c=1;
d=1;
N= f*b*c*d;
end
M=[4.5 10.5 20.5 25.6 30.8 34.6 35.8 38.4 17.2 18.5 20.5 22.6 24.5 26.6 28.9];
Y = (M-N);
A = sum(Y);
disp(A)
[EDITED, Jan, please apply a proper code formatting - Thanks]
  3 comentarios
dpb
dpb el 29 de Nov. de 2015
How is this any different from the previous question other than the length of the input vectors? <how-to-get-the-sum-of-the-difference-of-the-output-from-a-multiplication-and-a-data-set>?
As before, you've again got constants inside a looping structure which is of no use and I showed the individual results there the first time until you said it wasn't the answer were looking for.
As Jan says, you've got to first define the problem unequivocally if it isn't as give previously.
Thorsten
Thorsten el 30 de Nov. de 2015
Editada: Thorsten el 30 de Nov. de 2015
Could you provide a small example that shows the input and the expected result?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 29 de Nov. de 2015
A bold guess:
f = [1 4 8 10 14 18 22 26 30 34 38 42 45 48 52];
b = 2;
c = 1;
d = 1;
N = f*b*c*d;
M = [4.5, 10.5, 20.5, 25.6, 30.8, 34.6, 35.8, 38.4, 17.2, 18.5, 20.5, 22.6, ...
24.5, 26.6, 28.9];
Y = (M-N);
A = sum(Y);
disp(A)
  5 comentarios
segun
segun el 30 de Nov. de 2015
Editada: segun el 30 de Nov. de 2015
Please kindly check this code sent to me by somebody who i explained what i wanted to do with this style of coding as i know you are more experienced in MATLAB.
for f= [1 4 8 10 14 18 22 26 30 34 38 42 45 48 52]; b = 2; c = 1; d = 1; N =zeros; N = f*b*c*d; end M = [4.5, 10.5, 20.5, 25.6, 30.8, 34.6, 35.8, 38.4, 17.2, 18.5, 20.5, 22.6 ,24.5, 26.6, 28.9]; N= 0; for k =1:length(M); for i = 1:length(N); Y = sum(M-N) ; end end disp(Y)
dpb
dpb el 30 de Nov. de 2015
It doesn't help to explain non-working code with the same non-working code. We need to see what the answer that you think you should get is and how you arrived at it, not code that doesn't do what you intend it to do. Also, show what the end problem is for which the previous solution didn't work.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by