matlab gets different answers for the same code

13 Ansichten (letzte 30 Tage)
feynman feynman
feynman feynman am 10 Feb. 2020
Kommentiert: Stephen23 am 13 Feb. 2020
for loop=1:10
i12=randperm(n,2);
i1=i12(1);
i2=i12(2);
% i1=2;i2=6;
Floyd(i1,i2)
Dijkstra(i1,i2)
end
The above 2 algorithms (last 2 functions) are supposed to return the same answer and they usually do. But when I run for many loops, because of the rand numbers i1, i2 (by randperm), these 2 functions get different results. Strangely when i1 and i2 are fixed and randperm is removed, these 2 function 100% return the same result. Why?!
  12 Kommentare
feynman feynman
feynman feynman am 13 Feb. 2020
thx a lot adam! i didnt notice this obvious mistake. bug found!
Stephen23
Stephen23 am 13 Feb. 2020
The original question copied here, in case the OP deletes it like they have done with other questions:
"matlab gets different answers for the same code"
for loop=1:10
i12=randperm(n,2);
i1=i12(1);
i2=i12(2);
% i1=2;i2=6;
Floyd(i1,i2)
Dijkstra(i1,i2)
end
The above 2 algorithms (last 2 functions) are supposed to return the same answer and they usually do. But when I run for many loops, because of the rand numbers i1, i2 (by randperm), these 2 functions get different results. Strangely when i1 and i2 are fixed and randperm is removed, these 2 function 100% return the same result. Why?!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 11 Feb. 2020
Summary of comments under the question
The two functions being compared aren't necessarily doing the same thing though, with some sets of inputs, they do produce the same results.
Regarding the tests between randperm() and hard-coded, constant values, those values aren't the only variable that affects the outputs. Some reasons this may have been difficult to detect include the use of global variables, variable names that are difficult to distinguish, and multiple lines of code on the same line within the file.
  6 Kommentare
Walter Roberson
Walter Roberson am 13 Feb. 2020
The last large project I worked on, I had to spend several weeks rewriting the code to locate all the places I was reading and writing global variables and figuring out why an incompatibility was showing up. I ended up using accessor routines -- which still had the effect of changing global state, but in a more detectable way (and the code was made more disciplined about not requesting variables it did not strictly need.)
Global variables can be quite hard to debug with.
i have never trouble using them.
My father, who taught electrical and electronics, made sure I knew about turning devices off, and about grounding, and about always unplugging equipment you were working on ("just to be sure"), and I made sure to do all of that. Now, our clothes dryer had the tendency to melt one of the internal junctions, and he showed me how to fix it, and I always took all those precautions, along with being very careful in case the floor was wet. Nothing happened while he was still with us, and in the years later when I had to repair the dryer, nothing happened but I still made sure to take all the precautions and check them twice, to make sure that nothing would happen. All was well (other than the repair being a nuisance), and if someone else had done the same repair and not been as rigorous in precautions, chances are nothing would have happened to them.
Until, that is, the year when I took all those precautions, but got a rather nasty shock. This was 220 volt, 30 amp equipment. I couldn't move my hand away immediately -- AC locks your hand muscles. I thought I was probably done for.
Later, I checked and confirmed that I had taken all of the regular precautions.
What had happened? I'm not sure, but I realized that my father had forgotten to advise me that larger motors often have large capacitors that might need to be discharged.
Yes, when you use global variables you might never have had trouble using them. So far. But as professional programmers with decades of experience, we tell you that we have seen many a person laid low by global variables. There can be uses for global variables, but the people who get less afflicted by the problems with global, are the people who use the programming equivalents of grounding straps and shutting down the fuse box, taking deliberate preventitive steps to reduce the opportunity for problems.
Adam Danz
Adam Danz am 13 Feb. 2020
Bearbeitet: Adam Danz am 13 Feb. 2020
Very well-grounded advice!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Manual Performance Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by