Q:

Compute the closest distance between an input matrix with two others using matlab

If we consider 3 variables : x=1; y=2; and z=3;
To know if x is more closer to y than z , we must perform the following computations :
a=x-y;   % -1
a=a.^2;  % (-1).^2 = 1
b=x-z;   %-2
b=b.^2;  % (-2).^2 =4
%find the minimum
d=min(a,b);  % 1

Therefore, we notice that x is more closer to y than z.
Now, let us consider 3 matrices x,y and z instead of variables. It is the same concept but i am working with matrices instead of variables. 
Recall that i want to know if x is more closer to y or to z.
By the way, i tried to write the following matlab code:
  clear all;
close all;
clc;

%I have 3 matrices x, y and z
x=[1 4 1 ; 4 5 3 ; 7 3 9]
y=[3 6 5 ; 6 5 7 ; 3 2 3]
z=[2 6 5 ; 3 7 6 ; 2 7 6]

%Compute the distance between x and y
a= x-y;
%squaring this distance
a_squared=a.^2;

%Compute the distance between x and z
b=x-z;
%squaring the distance
b_squared=b.^2;

%So to compute the closest distance among a_squared and b_squared, i think
%that i should compute the minimum.

d=min(a_squared,b_squared); % in this case, we get a new matrix which is the minimum. So how can i know if this d belongs to the cluster y or z ?

in this case, we get a new matrix which is the minimum. So how can i know if this d belongs to the cluster y or z ? So regarding the value of d, how to know if d belongs to y or z ? In other words, how can i know if x is more closer to y or to z ?? Please i need your help and opinions. Any help will be very appreciated.
Thank's in advance.

A:

A distance can be defined as the norm of a difference.
For distances of matrices, you can define several distances depending on which matrix norm you use. Matlab's norm function can compute different norms, including the 2-norm and the Frobenius norm.
I'd recommend you start with the 2-norm and see whether this helps you do what you want to do.
Example code: Distance between matrices x and y:
dxy = norm(x - y, 2);

