0

I need to solve a min distance problem, to see some of the work which has being tried take a look at:

link: click here

I have four elements: two column vectors: alpha of dim (px1) and beta of dim (qx1). In this case p = q = 50 giving two column vectors of dim (50x1) each. They are defined as follows:

alpha = alpha = 0:0.05:2;
beta = beta = 0:0.05:2;

and I have two matrices: L1 and L2.

L1 is composed of three column-vectors of dimension (kx1) each.

L2 is composed of three column-vectors of dimension (mx1) each.

In this case, they have equal size, meaning that k = m = 1000 giving: L1 and L2 of dim (1000x3) each. The values of these matrices are predefined.

They have, nevertheless, the following structure:

L1(kx3) = [t1(kx1) t2(kx1) t3(kx1)];
L2(mx3) = [t1(mx1) t2(mx1) t3(mx1)];

The min. distance problem I need to solve is given (mathematically) as follows:

 d = min( (x-(alpha_p*t1_k - beta_q*t1_m)).^2 + (y-(alpha_p*t2_k - beta_q*t2_m)).^2 +
 (z-(alpha_p*t3_k - beta_q*t3_m)).^2 )

the values x,y,z are three fixed constants.

My problem

I need to develop an iteration which can give me back the index positions from the combination of: alpha, beta, L1 and L2 which fulfills the min-distance problem from above.

I hope the formulation for the problem is clear, I have been very careful with the index notations. But if it is still not so clear... the step size for:

alpha is p = 1,...50

beta is q = 1,...50

for L1; t1, t2, t3 is k = 1,...,1000

for L2; t1, t2, t3 is m = 1,...,1000

And I need to find the index of p, index of q, index of k and index of m which gives me the min. distance to the point x,y,z.

Thanks in advance for your help!

Community
  • 1
  • 1
Sergio Haram
  • 437
  • 4
  • 17
  • Do you know if there is a way to do it external to the `SO`? `t1,t2,t3` are (1000x3) that will be to much to post here.... or it may not? – Sergio Haram Jun 23 '14 at 10:29
  • What do you mean `external to the OP`? – Divakar Jun 23 '14 at 10:31
  • I meant the StackOverflow, I have fixed my last comment :) – Sergio Haram Jun 23 '14 at 10:34
  • `codereview.stackexchange.com` could be one place, but traffic there isn't much. – Divakar Jun 23 '14 at 10:36
  • What i don't understand: 1.) Is it right to use `t1_k` and `t1_m`? Because following your definition above those would be the same values all the time (same for `t2_m and t3_m`). I guess you meant to have 2 different arrays which correspond to the same kind of data but not the exact same data. 2.) You are sure, that you want the indices of: `p,q,k,m` ? Thus that you want to compare `50x50x1000x1000`? At least it sounds that way in your question. – The Minion Jun 23 '14 at 10:44
  • @TheMinion you are right! in this case the values are the same, the reason for this is that it will simplify the modeling immensely, on the other hand, they can have different values, but this will not affect the main result where I compute the min distance, because I at the end I am combining every single element (of `t1,t2,t3,alpha,beta`), in order to find the combination which gives the least distance. And the `50x50x1000x1000` sound correct too. Since the vectors and matrices are the same, **some** of the values when computing the min distance will be zero. But just some. – Sergio Haram Jun 23 '14 at 10:53
  • @SergioHaram If you know that some will be zero (or at least expect them to be) then what would you want as a result`? Because obviously all those values which return 0 have the same and smallest possible distance... – The Minion Jun 23 '14 at 10:55
  • @TheMinion agree! but the code has to be generic, meaning that should work with different values of `alpha` and `beta`. I was just trying to simplify the problem here. After the simplified problem is solve I could move to more difficult ones, but I need to solve the simplified one first, something that is giving me some headache. On the other hand, I need the index positions too. – Sergio Haram Jun 23 '14 at 11:00

1 Answers1

1

I don't know your values so i wasn't able to check my code. I am using loops because it is the most obvious solution. Pretty sure that someone from the bsxfun-brigarde ( ;-D ) will find a shorter/more effective solution.

alpha = 0:0.05:2;
beta = 0:0.05:2;

L1(kx3) = [t1(kx1) t2(kx1) t3(kx1)];
L2(mx3) = [t1(mx1) t2(mx1) t3(mx1)];
idx_smallest_d =[1,1,1,1];
smallest_d = min((x-(alpha(1)*t1(1) - beta(1)*t1(1))).^2 + (y-(alpha(1)*t2(1) - beta(1)*t2(1))).^2+...
                    (z-(alpha(1)*t3(1) - beta(1)*t3(1))).^2);

%The min. distance problem I need to solve is given (mathematically) as follows:
for p=1:1:50
    for q=1:1:50
        for k=1:1:1000
            for m=1:1:1000
                d = min((x-(alpha(p)*t1(k) - beta(q)*t1(m))).^2 + (y-(alpha(p)*t2(k) - beta(q)*t2(m))).^2+...
                    (z-(alpha(p)*t3(k) - beta(q)*t3(m))).^2);
                if d < smallest_d
                    smallest_d=d;
                    idx_smallest_d= [p,q,k,m];
                end
            end
        end
    end
end

What I am doing is predefining the smallest distance as the distance of the first combination and then checking for each combination rather the distance is smaller than the previous shortest distance.

The Minion
  • 1,164
  • 7
  • 16
  • thanks!!! :D I will try your code soon. I have actually `bsxfun` code implemented for this problem. (Divakar) is the one who deserves the honor for that. But the problem is that: when computing the multiplicity `(50x50x1000x1000)` you mentioned, `bsxfun` runs out of memory. So, let's see how the good old fashion for-loop approaches the problem :D. Thanks again! – Sergio Haram Jun 23 '14 at 11:32
  • Hi, Thanks again for your help, now, I am sorry but I have to disturb you with a question. In your code, when you are predefining the smallest distance. I have tried to just write a number (e.g. 100) but the code doesn't work, why? do I need to alway predefined this small distance with the vectors I am working with? Secondly, choosing `alpha(1) and beta(1)` will give me zero, this is form the values of my vectors, is this "safe?" or should I choose another values? I am working with this same problem, only that the loop is much more complex than this one, but... – Sergio Haram Jul 30 '14 at 13:25
  • the principle is the same(!) :) PS: no `bsxfun` implementation for this one so far. The loop works very well tough! – Sergio Haram Jul 30 '14 at 13:26
  • @SergioHaram You should definetely use the computation for the smallest distance rather than a fix value, because you dont know if there is a smaller distance than (100 your example) at all. By using the first distance you make sure the result is right. I don't really know about `alpha(1)` and `beta(1)` BUT in my opinion that should be ~=0 because otherwise you allways find the smallest possible distance (0) at step 1 and the code is unneccessary – The Minion Jul 30 '14 at 13:30
  • yes, I see what you are pointing out regarding alpha and beta being ´~=0´. And thanks for your quick answer, I will use the computation. My last question is: When I am predefining the smallest distance, I am choosing some entries from `alpha, beta` and the other vectors `L1, L2`. Now I am working with four vectors `L1, L2, L3, L4` alone, `alpha beta` is out. I have chose some entries from this new 4 vectors as well, in order to predefined the smallest distance, my question is: how to chose this `optimal` entry? Because `L1(1,1), L2(1,1), L3(1,1) and L4(1,1)`..... – Sergio Haram Jul 30 '14 at 13:41
  • may not be the smallest entries in these four vectors! Actually, in this case, the first entry in these four vectors are very large numbers... and I can not change anything about these four vectors... should I choose the smallest value-entry `L1, L2, L3 and L4` has?. I hope I am making clear my question. – Sergio Haram Jul 30 '14 at 13:42
  • That isn't neccessary. By using any distance of your values the code checks for each combination if it is "nearer" or not. If so it saves the new distance and keeps checking, til all combinations have been checked. Thus resulting in the smallest distance of your values. It doesn't matter if the starting distance is a high or small value compared to the other values, because it checks each combination anyway. And that one line write command won't have a big impact on runtime. – The Minion Jul 30 '14 at 13:56