How to vectorize a loop over rows ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello !
I've got the following code for Kaggle's digit recognizer using KNN, somehow, I unable to replace the following for loop into a vectorized implementation.
The loop is used to loop an entire row from the test data matrix.
knn_mat = zeros(m_test,1);
for i = 1:m_test
fprintf('i is %d \n',i);
compare_mat = repmat(x_test(i,:),m_train,1);
distance_mat = sum(power((compare_mat - x_train),2),2);
[a,b] = min(distance_mat);
knn_mat(i) = y(b);
end
Thank You !
10 commentaires
Matz Johansson Bergström
le 19 Juil 2014
I think that another type of approach or calling compiled code would be of better help. I'm thinking like storing x_train in a spatial data structure to be able to quickly find the closest neighbour of x_test in x_train.
Réponse acceptée
Jan
le 19 Juil 2014
Modifié(e) : Jan
le 19 Juil 2014
This is about 25% faster for your small test data file:
knn_mat = zeros(m_test,1);
for i = 1:m_test
distance_mat = sum(bsxfun(@minus, x_test(i,:), x_train).^2, 2);
[a,b] = min(distance_mat);
knn_mat(i) = y(b);
end
The creation of the large intermediate array x_test-x_train might be the bottleneck here for larger arrays. A complete vectorization would increase the problem, most of all, if the data size is larger.
Then a C-Mex would be much faster. Are you familiar with writing C-Mex functions?
1 commentaire
Plus de réponses (1)
Matz Johansson Bergström
le 20 Juil 2014
Jan: Nice. That was actually my first thought, but I only used it on x_test (regged and downloaded from Kaggle before Karan uploaded them) and then I only get 5-8% speedup, unfortunately.
Karan: So, no, as I mentioned earlier, compiled code is the way to go here, there is no simple way you can vectorize this code inside of Matlab.
If you wish to call compiled code from Matlab you can, as Jan states, use C-mex. I would write the code directly in C AND/OR process the data a little maybe? For more information about C-mex, see link to documentation.
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!