Filter löschen
Filter löschen

How to vectorize computing all pairwise vectors in a pointcloud?

2 Ansichten (letzte 30 Tage)
Burke Rosen
Burke Rosen am 23 Mär. 2017
Beantwortet: Walter Roberson am 23 Mär. 2017
I have an m points x n dimensions array xy of Cartesian coordinates. In my case, n is 2, but this should generalize to at least three dimensions. My goal is get the all the vectors created by combinatorial pairs of points. I can do this with nested for loops (see below), how can I vectorize this operation? I can get the vector magnitudes with pdist2(xy,xy), but not the vector angles.
[m,n] = size(xy);
V = zeros(m,m,n);
for im1 = 1:m
for im2 = 1:m
V(im1,im2,:) = xy(im1,:) - xy(im2,:);
end
end

Antworten (3)

Burke Rosen
Burke Rosen am 23 Mär. 2017
After further review, I reduced it down to:
[m,n] = size(xy);
V = zeros(m,m,n);
for im = 1:m
V(:,im,:) = xy - xy(im,:); %2016b bsxfun syntax
end
Can it be vectorized any further?

Lowey
Lowey am 23 Mär. 2017
If I understand your question correctly, the meshgrid function may be of help.
im1 = 1:m;
im2 = 1:m;
[X, Y] = meshgrid(im1, im2);
The elements in X and their corresponding elements in Y give all possible combinations

Walter Roberson
Walter Roberson am 23 Mär. 2017
V = pdist(xy);

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by