Norm of the difference of each row of two matrices of different dimensions
Show older comments
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.
Accepted Answer
More Answers (3)
Teja Muppirala
on 14 Apr 2011
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
1 Comment
Sudarshan
on 14 Apr 2011
Sudarshan
on 14 Apr 2011
0 votes
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!