MATLAB: Pdist2 inside for

for loopMATLABmatrixpdist

Hi everybody,
i have two 3D matrix A and B with different lengths. I need to build a for loop to calculate the pdist2 between the first row of A and all the rows of B, the second row of A and all the rows of B, …., the n row of A and all the rows of B.
An example of the two matrix:
A
X Y Z
0.2 0.4 0.5
1.4 0.6 4.5
0.3 0.5 3.1
B
X Y Z
0.8 0.7 0.5
1.4 0.9 1.5
0.2 3.5 2.1
0.3 0.7 0.5
2.4 0.3 0.5
0.2 3.5 3.1
Thank you very much!

Best Answer

Why? pdist2 works directly with providing a [M1 x N] and a [M2 x N] matrix. So why do you want to write a loop and process one matrix rowwise?
D = pdist2(A, B)
But you write "3D matrix". Maybe you mean "3D array", because matrices are 2D by definition. Then the shown examples are misleading. If A and B are 3D arrays, you for got to mention what you want to do with the 3rd dimension.