Borrar filtros
Borrar filtros

loop on matrix array

4 visualizaciones (últimos 30 días)
tilfani oussama
tilfani oussama el 25 de Oct. de 2022
Editada: Torsten el 25 de Oct. de 2022
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 comentarios
Dyuman Joshi
Dyuman Joshi el 25 de Oct. de 2022
Can you give some data? Input, result and expected output?
tilfani oussama
tilfani oussama el 25 de Oct. de 2022
In the file attached an example of data, we are reasoning on culumns of the matrix. And expected results like the second sheet

Iniciar sesión para comentar.

Respuestas (1)

Torsten
Torsten el 25 de Oct. de 2022
Editada: Torsten el 25 de Oct. de 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.'))
D = 3×3
0 4.1231 10.8628 4.1231 0 9.0000 10.8628 9.0000 0
  2 comentarios
tilfani oussama
tilfani oussama el 25 de Oct. de 2022
Thank you for replying. However, I would like to estimate another distance than the euclidean, which doesn't exist in matlab. I did it as a function.
How to make a loop that allows to make the calculation through all arrays as indicated in my question.
Torsten
Torsten el 25 de Oct. de 2022
Editada: Torsten el 25 de Oct. de 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
d = 3×3
0 4.1231 10.8628 4.1231 0 9.0000 10.8628 9.0000 0

Iniciar sesión para comentar.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by