loop on matrix array
1 view (last 30 days)
Show older comments
I would like to make a loop that computes a distance between all matrix arrays, and save them in a distance matrix. The distance is defined in a separate function
Let consider a Matrix A=[A1,A2,...,An]. The first iteration will computes distance between A1 and (n-1) arrays, the second between A2 and the rest and so on.
For the first, the loop is (j=1)
for i=1:n-1
d(i,j)=f(di,di+1);
end
The second iteration starts from i=2. and so on. The diagonal is zero.
I made the following loop, but it doesn't work.
for i=1:n-1
for k=i:n-1
d(k,i)=f(dk,dk+1);
end
end
Can someone help?
4 Comments
Answers (1)
Torsten
on 25 Oct 2022
Edited: Torsten
on 25 Oct 2022
The following code computes the distance between the columns of M.
M = [0 3 -1
4 6 13
-2 0 4];
D = squareform(pdist(M.'))
2 Comments
Torsten
on 25 Oct 2022
Edited: Torsten
on 25 Oct 2022
There is a choice between a large number of distances for "pdist".
You can even include your own anonymous distance function in the call to "pdist".
From the documentation:
'euclidean'
Euclidean distance (default).
'squaredeuclidean'
Squared Euclidean distance. (This option is provided for efficiency only. It does not satisfy the triangle inequality.)
'seuclidean'
Standardized Euclidean distance. Each coordinate difference between observations is scaled by dividing by the corresponding element of the standard deviation, S = std(X,'omitnan'). Use DistParameter to specify another value for S.
'mahalanobis'
Mahalanobis distance using the sample covariance of X, C = cov(X,'omitrows'). Use DistParameter to specify another value for C, where the matrix C is symmetric and positive definite.
'cityblock'
City block distance.
'minkowski'
Minkowski distance. The default exponent is 2. Use DistParameter to specify a different exponent P, where P is a positive scalar value of the exponent.
'chebychev'
Chebychev distance (maximum coordinate difference).
'cosine'
One minus the cosine of the included angle between points (treated as vectors).
'correlation'
One minus the sample correlation between points (treated as sequences of values).
'hamming'
Hamming distance, which is the percentage of coordinates that differ.
'jaccard'
One minus the Jaccard coefficient, which is the percentage of nonzero coordinates that differ.
'spearman'
One minus the sample Spearman's rank correlation between observations (treated as sequences of values).
@distfun
Custom distance function handle. A distance function has the form
function D2 = distfun(ZI,ZJ)
% calculation of distance
...where
- ZI is a 1-by-n vector containing a single observation.
- ZJ is an m2-by-n matrix containing multiple observations. distfun must accept a matrix ZJ with an arbitrary number of observations.
- D2 is an m2-by-1 vector of distances, and D2(k) is the distance between observations ZI and ZJ(k,:).
If your data is not sparse, you can generally compute distance more quickly by using a built-in distance instead of a function handle.
If you still want to use a for loop, here is a code:
M = [0 3 -1
4 6 13
-2 0 4];
f = @(x,y)norm(x-y);
n = size(M,2);
d = zeros(n);
for i = 1:n
for j = i+1:n
d(i,j) = f(M(:,i),M(:,j));
end
end
for i = 1:n
for j = 1:i-1
d(i,j) = d(j,i);
end
end
d
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!