Problem with adding integers
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joel Schelander
le 21 Avr 2021
Réponse apportée : Steven Lord
le 21 Avr 2021
I am running 36 functions. At the 17th I get an error at this part of a loop:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)})+double(Vehicle17{index(17)}))./max(HH2);
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
The thing is, running the functions before works fine, the same part in the 16th function looks like this:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+double(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)}))./max(HH2);
All Vehicle(nr) are 1x108 cells containing 108 52560zx1 int8, index is a 1x17 double, HH2 is a 52560x1 double. I do not understand whats wrong. Any ideas?
2 commentaires
David Hill
le 21 Avr 2021
I would stop the application on that line and then run each term of your equation until you find your problem.
Réponse acceptée
Plus de réponses (1)
Steven Lord
le 21 Avr 2021
Set an error breakpoint and run your code. When you reach the line that throws the error, check that what you believe to be true about the classes of the variables used on that line is actually true.
As an aside, this:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)})+double(Vehicle17{index(17)}))./max(HH2);
smells like week-old fish to me. [Do you really want to type this out for say 50, 100, 1000, or more vehicles?]
The general consensus is that using variables named Vehicle1, Vehicle2, Vehicle3, ... is strongly discouraged as an anti-pattern. "Repeating yourself" or "Copy and paste programming" seem the most applicable. I'd probably use an N-dimensional array or maybe a 2-dimensional cell array instead.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!