0
votes

I'm trying to compute two matrices , one of which is 400*2 and the other being 20*20. The first one contains x and y coordinates of 400 points and the second being another 400 points with x's being 2nd(i,i) and y's being (i,k) both i and k's are 20 number counters.I'm trying to compute the distances between those 2 matrix points which would give me 400*400 matrix. The code i'm using is ;

for i=1:20
    for j=1:400
        for k=1:20
        L(j,)=sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end

C being the first matrix and M being the second. Now i know i made it sound a little hard but the trouble is i cant find a counter to give to L(j, ) that part. If you guys have any idea please do tell...

Edit: Well first of all think there is 2 point vectors. X's being -0.95:0.1:0.95 and y's are the same. M vector is that all of the x's paired with a Y so it would make 20*20 matrix or a 400*2 however i could not make it work like a 400*2 so have gone with 20*20.The matrix is a little hard for me to do so my indexing of M shows that.If you got a better way to pair them i would appreciate it as well.

My matrix looks like this ;

-0.95 -0.85 -0.75...
-0.95 -0.85 -0.75 ... 
.
.
.
2
I think the question would be more understandable if you include actual examples of some portions of the matrices. - David Alber
You description of matrix M is not clear. Please clarify what its entries represent. - Phonon

2 Answers

3
votes

The above code should be very slow... Using a lot of for-loop is not "matlab-style" What I would do is:

Mdiag=repmat(diag(M)',20,1);
L=pdist2(C,[Mdiag(:) M(:)]);

The first line extracts the diagonal terms of M and repeat them.

After that [Mdiag(:) M(:)] is a standard form of your second set of points.

pdist2 is a function that compute all the pairwise distances.

0
votes

Does it matter what order they are in? I am assuming it doesn't, so long as you know what that order is. I'd use this:

for i=1:20
    for j=1:400
        for k=1:20
            L(j,(i-1)*20+k) = sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end