Norm of the difference of each row of two matrices of different dimensions
古いコメントを表示
Hello, I have two matrices A and B of dimensions m-by-3 and n-by-3 respectively where n < m (they are basically RGB values of an image). For sake of simplicity assume m = 8 and n = 4. For each row in m, I need to calculate the the norm of the difference of that row with the each row of n. The way I am doing it now is using a nested for loop, but my m and n are way higher. So this is taking forever to run. I was wondering if there is any other efficient way to do it.
Just for the sake of completion here is my code for performing this operation:
for i = 1:size(m, 1)
for j = 1:size(n, 1)
d(j, 1) = norm(m(i, 1) - n(j, 1))
end
% I do some stuff with d here, but its not important to the
question.
end
Thanks.
採用された回答
その他の回答 (3 件)
Teja Muppirala
2011 年 4 月 14 日
What's wrong with BSXFUN here?
for pix = 1:npixels
dist = sum(bsxfun(@minus,means,yClustered(pix, 1:end-1)).^2,2);
[mDist, mIdx] = min(dist);
yClustered(pix, 4) = mIdx;
means(mIdx, :) = mean(yClustered(yClustered(:, end) == mIdx, 1:end-1));
end
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!