MATLAB's 'pdist2' function is giving inconsistent results with custom distance metrics in R2023b
I need some guidance on I'm trying to calculate pairwise distances between two sets of points using the `pdist2` function in MATLAB R2023b, but I'm encountering some unexpected results when I use a custom distance metric. Specifically, I've implemented a simple custom metric that computes the Manhattan distance, but it doesn't seem to be calculating correctly for certain point combinations. Here's a snippet of the code I've written: ```matlab % Sample data A = [1, 2; 3, 4; 5, 6]; B = [1, 1; 4, 5]; % Custom Manhattan distance function function d = manhattan_dist(x, y) d = sum(abs(x - y)); end % Using pdist2 with the custom metric D = pdist2(A, B, @manhattan_dist); ``` When I run the code, I expect the output matrix `D` to contain the pairwise Manhattan distances, but I get some results that seem incorrect. For instance, the distance between the first point in `A` (1, 2) and the first point in `B` (1, 1) should be 1, but it returns 0. Also, the distance from (3, 4) to (4, 5) should be 2, yet it returns 3. I've verified the custom function separately: ```matlab manhattan_dist([1, 2], [1, 1]) % Should return 1 manhattan_dist([3, 4], [4, 5]) % Should return 2 ``` Both of these return the expected results. I’ve also checked that `A` and `B` are formatted correctly and do not contain any NaN values. Is there something I’m missing when passing the custom function to `pdist2`, or is there a limitation in how it handles custom metrics? Any insights or suggestions on how to troubleshoot this issue would be greatly appreciated!