Tranform Python code in Matlab

1 vue (au cours des 30 derniers jours)
Example Mo le 20 Juin 2015
Commenté : Example Mo le 21 Juin 2015
Hello, I'm new to Matlab and I want to transform a simple python code.I tried to do that alone but I don't know how to make array indexation, in this case X[pairs[:,0],:] . Here is my code:
def learning_distance_symmetric(W,X,pairs,batch_size=10000):
"""Compute an array of Euclidean distances between points indexed by pairs
To make it memory-efficient, we compute the array in several batches.
Parameters
----------
X : array, shape (n_samples, n_features)
Data matrix
W : array, shape (n_features,n_features)
Learned distance matrix
pairs : array, shape (n_pairs, 2)
Pair indices
batch_size : int
Batch size (the smaller, the slower but less memory intensive)
Output
------
dist : array, shape (n_pairs,)
The array of distances
"""
n_pairs = pairs.shape[0]
dist = np.ones((n_pairs,), dtype=np.dtype("float32"))
for a in range(0, n_pairs, batch_size):
b = min(a + batch_size, n_pairs)
diff = X[pairs[:,0],:] - X[pairs[:,1],:]
dist[a:b] = np.sum(np.dot(W,diff.T)*diff.T, axis=0)
return dist
Someone could give me some clues (Maybe the documentation dealing with this case) ? Thanks in advance.
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponses (1)

Ken Atwell le 21 Juin 2015
MATLAB is 1-indexed, so I think you're looking for something like:
diff = X(pairs(:,1),:) - X(pairs(:,2),:);
1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Example Mo le 21 Juin 2015
Thanks, it was exactly what I'm looking for.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by