Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 29 Nov 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 il 30 Nov 2015
Modificato: Thorsten il 30 Nov 2015
Could you provide a small example that shows the input and the expected result?

Accedi per commentare.

Risposte (1)

Jan
Jan il 29 Nov 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 Commenti
segun
segun il 30 Nov 2015
Modificato: segun il 30 Nov 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 il 30 Nov 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.

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by