matlab gets different answers for the same code
Show older comments
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 Comments
feynman feynman
on 11 Feb 2020
Walter Roberson
on 11 Feb 2020
We need the code for them.
Do you really expect that someone could diagnose why two unfamiliar functions produce different results given the same inputs without seeing what the functions are doing? It could be because...
- ...the function are doing different things and coincidentally have outputs that appear to be the same for some inputs.
- ...rounding error
- ...at least one function relies on a random process
- ...your analysis of the results are incorrect and they do produce the same outputs
I also see that you aren't storing the outputs for each iteration and I wonder if you're just comparing the final results. The problem is obviously either with your expectations for these functions, the code within the functions, or your analysis of the results. Without a minimal working example we cannot help you.
I'm assuming the "right" and "wrong" functions are the Floyd and Dijkstra functions.
Did you write both of these functions or did you get them from somewhere?
When I run the code, on some iterations the dis1 and dis2 outputs match and sometimes they don't.
dis1-dis2
ans =
Columns 1 through 8
0 0 0 0 0 0 0.083754 0.3272
Columns 9 through 10
0.00029923 0.46701
What makes you think they should always match? I'm not going to dissect each function to identify where the algorithms bifurcate but clearly they are two different algorithms that behave slightly different for some sets of inputs.
feynman feynman
on 11 Feb 2020
Edited: feynman feynman
on 11 Feb 2020
Adam Danz
on 11 Feb 2020
The code is very hard to debug because of some bad practices.
Never use global variables. Instead, pass those variables into the functions as additional inputs.
Avoid putting several executable lines of code in the same line of the file (ie: nodesSub=nodes;nodesSub(ii)=[]). It might make sense to you but will greately decreases readability when others look at your code.
Variable names such as i12 i1 i2 ii are difficult to distinguish.
When you run the code with randperm(), on each iteration the i12, i1, i2 W, and nodes variables change.
When you run with the fixed values for i1 and i2, the w and nodes variables still change but i1 and i2 do not. That's comparing apples to oranges.
Here's a list of i12 values that were produced by randperm() for 10 iterations
randPermValues = [
8 7
1 7
5 6
5 4
7 6
5 2
2 8
6 2
5 7
5 2];
They resulted in the following dis1 dis2 differences.
>> dis1-dis2
ans =
Columns 1 through 8
0 0 0.2041 0.13379 0.24726 0.019765 0.40222 0
Columns 9 through 10
0 0
Now run those same, hard-coded values.
clear;clc;
global process% print intermediate computations
process=1*0;
n=8;% # nodes
nodes=1:n;% imtermediate nodes
randPermValues = [
8 7
1 7
5 6
5 4
7 6
5 2
2 8
6 2
5 7
5 2];
for loop=1:10
% i12=randperm(n,2);
i12 = randPermValues(loop,:);
i1=i12(1);
i2=i12(2);% start and end
% i1=2;i2=6;
global W% weight matrix
W=rand(n);
W=W-diag(diag(W));% Wii=0
% remove start and end from nodes for my code
for i=1:length(nodes)
if nodes(i)==i1
nodes(i)=[];break;
end
end
for i=1:length(nodes)
if nodes(i)==i2
nodes(i)=[];break;
end
end
[dis1(loop),route1{loop}]=wrong(i1,nodes,i2)
[dis2(loop),route2{loop}]=right(i1,i2)
end
And you'll see that there are still differences
dis1-dis2
ans =
Columns 1 through 8
0 0 0 0 0.1659 0 0 0
Columns 9 through 10
0.39936 0
Adam Danz
on 11 Feb 2020
With fixed values for i1 and i2, nodes doesn't change. However, suppose on iteration number i the i1 and i2 values are 5 and 6 create node values of [2 3 4] (I'm making these numbers up). That doesn't mean that every time i1 and i2 are 5 and 6, the nodes will always be [2 3 4].
Image Analyst
on 11 Feb 2020
Can someone post an official Answer for this below in the official Answer section?
Adam Danz
on 12 Feb 2020
"my 'nodes' is always 1:n subtracted by i1 and i2"
No, that's not true. Run the randperm() version and print out the results of nodes.
nodes =
1 2 5 6 7 8
nodes =
2 5 6 7
nodes =
2 6 7
nodes =
6 7
nodes =
6 7
nodes =
6
The nodes are always the same when the i1 and i2 are constant but when you use randperm(), i1 and i2 are not constant.
feynman feynman
on 13 Feb 2020
Stephen23
on 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?!
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!