Info

此问题已关闭。 请重新打开它进行编辑或回答。

Why am I getting "Array indices must be positive integers or logical values" when trying to add together all the integers?

1 次查看(过去 30 天)
So here is the Data set:
Cost=[
55.58
55.61
55.64
55.69
55.73
55.78
55.82
55.88
55.94
56.02
56.09
56.18
56.29
56.39
56.50
56.63
56.78
56.93
57.11
57.31
57.52
57.76
58.01
58.30
58.61
58.97
59.36
59.78
60.26
60.79
61.36
62.00
62.71
63.49
64.36
65.32
66.37
67.54
68.83
70.25
62.66
59.27
65.33
57.82
64.55
57.76
60.25
63.71
65.81
56.36
67.82
65.75
61.85
61.16
61.31
59.41
62.14
62.17
66.31
66.01
63.97
60.38
66.23
62.47
60.01
67.96
67.10
62.71
63.68
63.20
58.07
59.35
61.63
58.39
66.67
57.91
58.33
57.58
58.34
61.16
62.02
60.74
59.59
58.58
57.70
56.96
56.35
55.88
55.54
55.34
55.27
55.34
55.54
55.88
56.35
56.96
57.70
58.58
59.59];
C=sum(Cost)
When I run it, I keep getting errors. I've used other commands to find the max or min and they come out just fine. Thank you

回答(2 个)

Guillaume
Guillaume 2019-12-6
Most likely you've created a variable called sum, so instead of summing the elements of Cost, you're using Cost as an index into the sum variable and of course as the error message tells you values like 55.58 are not valid indices.
Don't use sum, max, min, diff, etc. as variable names if you want to avoid this kind of problem.

Image Analyst
Image Analyst 2019-12-6
This is a VERY frequently asked question. So see the FAQ.
Your code runs fine though. I copied and pasted and got htis:
C =
5937.99
so something else must be wrong. Please give the COMPLETE error message, AFTER you read this link.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by