0
|
6694 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
GROUPS:

# Help with ManhattanDistance function

Posted 9 years ago
 Hello friends I'm working on a mini project (8-puzzle) for which I need the distance manhattan and have programmed it in the following way valabs[a_, b_] := If [a > b, a - b, b - a] manhattan[tablero_List] := Module[{i, j, x, y, cuenta = 0}, For[i = 1, i <= 3, i++, For[j = 1, j <= 3, j++, If[tablero[[i, j]] == 0 , cuenta += 0; , x = Quotient[tablero[[i, j]] - 1, 3] + 1; y = Mod[tablero[[i, j]] - 1, 3] + 1; cuenta = cuenta + valabs[x, i] + valabs[y, j]; ]; ]; ]; cuenta ]  Remember that you should not count zero, wanting to do the same with function ManhattanDistance get a different value, here is an example. With my function In:= manhattan[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}}] Out= 13  Now with ManhattanDistance In:= ManhattanDistance[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}}, {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}] Out= 11  Someone could say why am I having such a result, I think that there is some trick to be able to do the same, it would be of much help some guidance, because I plan to keep using ManhattanDistance later in my code, greetings to all.
4 Replies
Sort By:
Posted 9 years ago
 I would like to apologize for being so brief in my explanation, but I still insist that the function ManhattanDistances gives me a different result, in my function manhattan, the distance manhattan is calculated between the State that serves as an argument to the function and the state goal { {1,2,3 } , {4,5,6 } , {7,8,0 }} so I make the comparison with ManhattanDistances[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}},{ {1,2,3 } , {4,5,6 } , {7,8,0 }} ], so will look the result that is different in both cases.Or I could suggest some improvements to the function I les shared, as wanting to change the for table problems arise me, I hope to have explained well my idea and I hope your comments.In the section of Consistency and Admissibility the next article explains what I need http://en.wikipedia.org/wiki/Heuristic_function
Posted 9 years ago
 I would like to apologize for being so brief in my explanation, but I still insist that the function ManhattanDistances gives me a different result, in my function manhattan, the distance manhattan is calculated between the State that serves as an argument to the function and the state goal { {1,2,3 } , {4,5,6 } , {7,8,0 }} so I make the comparison with ManhattanDistances[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}},{ {1,2,3 } , {4,5,6 } , {7,8,0 }} ], so will look the result that is different in both cases.Or I could suggest some improvements to the function I les shared, as wanting to change the for table problems arise me, I hope to have explained well my idea and I hope your comments.
Posted 9 years ago
 ManhattanDistance, applied to matrices, produces the l1 operator norm of their difference.  Norm[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}} - {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}, 1] (* Out= 11 *) I was not able to figure out what manhattan is computing.
Posted 9 years ago
 Norm[expr,1] gives the l1 norm of expr. That's the Manhattan distance.