# is there an alternative to pdist2 that calculates the distance between a matrices with different column numbers

6 views (last 30 days)
James Peach on 4 Sep 2020
Commented: James Peach on 5 Sep 2020
I was wondering if there is a built in matlab fucntion that calculates the distance between two arrays that don't have the same column number like in pdist2? For example if matrix A was 102x3 and Matrix B was 3x15, is there a MATLAB function that can do this calculation for me or do I need to use nested for loops?

Adam Danz on 4 Sep 2020
Edited: Adam Danz on 4 Sep 2020
You just need to transpose B
d = pdist2(A, B');
d(i,j) is the distance between A(i,:) and B(:,j).

Show 1 older comment
John D'Errico on 5 Sep 2020
Adam did not tell you to transpose the array and save it as such, replacing the original array. He said to transpose it inline, as:
d = pdist2(A, B');
That changes nothing about the original array B for the next time you will use it.
Adam Danz on 5 Sep 2020
As evidence of what John said, note the indexing I used in my answer to describe d.
d(i,j) is the distance between A(i,:) and B(:,j).
James Peach on 5 Sep 2020
okay I see tha thanks for your help.

R2019a

### Community Treasure Hunt

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

Start Hunting!