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

1 次查看(过去 30 天)
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 个评论
dpb
dpb 2015-11-29
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 2015-11-30
编辑:Thorsten 2015-11-30
Could you provide a small example that shows the input and the expected result?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2015-11-29
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 个评论
segun
segun 2015-11-30
编辑:segun 2015-11-30
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 2015-11-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by