2

I have distributed 100 random points(nodes) in an area by using following code

n=100; xm=100;ym=100;
    for i = 1 : 1 :n
    S(i).xd = rand(1,1)*xm ;
    S(i).yd = rand (1,1)*ym ;
    xd(i) = S(i).xd ;
    yd(i) = S(i).yd ;
    S(i).id = i;
    S(i).type = 0;
    S(i).g = 0 ;
    S(i).E = Eo ;
    S(i).type = 0 ;

end

Next I have fixed 10 gateway nodes at the edge of the area using following code

 for i=1:1:10
    Sg(i).xd= 2+100*rand(1,1);
      Sg(i).yd=100;
    xd(i)=Sg(i).xd;
    yd(i)=Sg(i).yd;
    Sg(i).id=i;
     plot(Sg(i).xd,Sg(i).yd,'*k')
grid on;
hold on;   
end

Now I have formed cluster heads using LEACH protocol from 100 nodes.

I have to find min distance gateway node from a CH. As there are 10 gateway nodes, I have to find which is closer to a CH in the area.

To determine CH I have used following code

for all nodes in the area


    temp_rand1=rand;
                if(temp_rand1<= (p/(1-p*mod(r,round(1/p)))))
                    countCHs1=countCHs1+1;

                    S3(i).type=1;
                    S3(i).g=round(1/p)-1;
                    C1(cluster1).xd=S3(i).xd;
                    C1(cluster1).yd=S3(i).yd;

         **distance1=sqrt((S3(i).xd-(gw_node.x) )^2 + (S3(i).yd-(gw_node.y) )^2 );**
                    C1(cluster1).distance1=distance1;
                    C1(cluster1).id=i;
                    X(cluster1)=S3(i).xd;
                    Y(cluster1)=S3(i).yd;
                    cluster1=cluster1+1; 
end

I know how to determine distance between a CH and one gateway node, But I do not know how to find closet gateway node from a set of gateway nodes. Thanks in advance

Please reply

gariepy
  • 3,576
  • 6
  • 21
  • 34
Aska
  • 23
  • 5
  • 1
    Could you post some sample data and expected output, please. I know there is code there to generate sample data but those will be different every time it is run and difficult to compare it with your expected outcome. It's much better if you fix the data yourself. – kkuilla Apr 28 '15 at 08:24

2 Answers2

0

Not too sure what you are asking for but I think you want to find the two closest points.

This is how you could do it.

%// Generate some points
n = 3;    xm = 1:n;    ym =1:n;
m = [xm' ym']; %// This is the line
xd = [    2    8   3]; 
yd = [    1    4   3];
d = [xd' yd']; %// These are the random points
drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1); 
mrep = kron(m,B); %// This will repeat each value in m n times        
distances = bsxfun(@minus,mrep,drep);  %// The distance from each point to every other point       
C_squared = sum(distances.^2,2)
[min_dist,idx] = min(sum(distances.^2,2)) %// Find the minimum distance

value_in_m = mrep(idx,:)

ans =

   3   3

value_in_d = drep(idx,:)
ans =

   3   3

Explanation

% Generate some points
n = 3;
xm = 1:n;
ym =1:n;
m = [xm' ym']; %// This is the line. Each row is a point
xd = [    2    8   3]; 
yd = [    1    4   3];
d = [xd' yd']; %// These are the random points

We want to calculate the distance from one point to every other point so we repeat the points nxn times

Below is just one way of getting repeated elements. More suggestions are here: https://stackoverflow.com/a/16269185

drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1); 
mrep = kron(m,B); %// This will repeat each value in m n times

We are basing this on Pythagoras theorem so (m_x-d_x)^2 + (m_y-d_y)^2 = C^2

distances = bsxfun(@minus,mrep,drep);  %// The distance from each point to every other point       
C_squared = sum(distances.^2,2)

Find the minimum distance

[min_dist,idx] = min(sum(distances.^2,2)); 

Find the points with the minimum distance

value_in_m = mrep(idx,:)
ans =

   3   3

value_in_d = drep(idx,:)
ans =

   3   3
Community
  • 1
  • 1
kkuilla
  • 2,226
  • 3
  • 34
  • 37
  • Apologies but I don't understand why that is a problem. If I understand your question correctly, you want to find the two points that are closest. The fact that the points are randomly distributed does not matter. Unless you mean that the random points are drawn from a normal distribution and you want to know the distance in terms of probability. – kkuilla Apr 28 '15 at 12:25
  • @kkulia Please advise me how to code my comments. As I am not able to post answer here. I have to write some code. But how should I frame it in code format? – Aska Apr 28 '15 at 12:43
  • Unfortunately, your comments make no sense to me. You post some code and say that the code is your main problem but you don't state what is wrong. It is much better if you update your question. I have read your question and answered it to the best of my knowledge. Please update your question with relevant information. The guides to [How To Ask](http://stackoverflow.com/help/how-to-ask) and [mcve](http://stackoverflow.com/help/mcve) might be useful reads... – kkuilla Apr 28 '15 at 12:50
0

Not sure what you are asking. I assume you have set of fixed points in a straight line (marked blue) and set of random points (marked red). You want the Minimal distance between one fixed and one random point.

Try this code:

si = randi([0,100],10,2);    %// Random points (red)
sg(:,1) = randi([0,100],10,1);  %// fixed points x param (blue)
sg(:,2) = ones(10,1)*50;    %// fixed points y param (blue) r const, to make them a st. line

sgc = mat2cell(sg,ones(1,size(sg,1)),size(sg,2)); %// converting to cell array
sic = mat2cell(si,ones(1,size(si,1)),size(si,2)); %// converting to cell array

%// distMat gives n*n matrix of all the dist between each fixed point with every other point.
distMat = cellfun(@(x,y) pdist2(x,y),repmat(sic,1,size(sgc,1)),repmat(sgc',size(sic,1),1));

figure; axis('image');
plot(si(:,1),si(:,2),'r.');
text(si(:,1),si(:,2),[repmat('  ',size(si,1),1), num2str((1:size(si,1))')]);
hold on
plot(sg(:,1),sg(:,2),'b.');
text(sg(:,1),sg(:,2),[repmat('  ',size(sg,1),1), num2str((1:size(sg,1))')]);
hold off

[mdist, index] = min(distMat(:));
[randomP, fixedP] = ind2sub(size(distMat),index)

Plot:

enter image description here

output:

randomP =

 2


fixedP =

 2

>> mdist

mdist =

6.7082
Santhan Salai
  • 3,888
  • 19
  • 29
  • Thanks a lot. your answer is near to my question. Please once again read my question and reply. – Aska Apr 28 '15 at 13:12
  • Please replace 50 by 100 in 3rd line of your code. Suppose 7 is a random point. 2,5, 8 are fixed point. I have to find which fixed point (out of 2,5,8) is closet to random point 7. Coordinate of that fixed point must be stored in a variable. – Aska Apr 28 '15 at 13:20
  • The above code is a good one. It is finding distance between one random point and one fixed point. What if I want to find min distance between all random point and fix points. e.g. If there are 5 random point and 5 fixed point. Then which random point is close to which fixed point. Actually I have to find gateway nodes (fixed point) for Cluster heads(random point) with min distance. – Aska Apr 28 '15 at 13:32
  • @Aska, actually the code is finding minimum distance between all random point and fix points. See the plot. I found out that the minimum distance happens between fixed point 2 and random point 2. So it shows the distance between them. I took 10 random points and 10 fixed points – Santhan Salai Apr 28 '15 at 13:51
  • @Aska print `distMat` to see the distance matrix. Each element in the matrix represents dist between a fixed point(column) and a random point(row). – Santhan Salai Apr 28 '15 at 13:55
  • 1
    Thanks a lot again. Yes it is giving distance between all points. Now I have to make use of this in my code. – Aska Apr 28 '15 at 16:21
  • Saalai One more simple question. If I want to change number of fixed point from 10 to 16 then what changes should I made in the code? – Aska Apr 28 '15 at 17:54
  • @Aska In the second and third line replace, `10` with `16`. thats it :) – Santhan Salai Apr 29 '15 at 00:37
  • 1
    :) Thanks. Its working. How to remove the numbers from image. I just want blue and red dots on image. – Aska Apr 29 '15 at 04:13
  • Also some of these numbers are overlapping. Can we keep them apart by a distance of 2. As I have done in my code. Sg(i).xd= 2+100*rand(1,1); – Aska Apr 29 '15 at 04:29
  • @Aska, in the line starting with text, you could find spaces between quotes. reduce its space to one. It should work – Santhan Salai Apr 29 '15 at 04:35
  • How to remove numbers form the image? I just want nodes. Also Please help me in using the min distance in my code. I have to find min distance between Chs and gateway nodes. How shold I use distMat in my code? Thanks – Aska Apr 29 '15 at 11:18
  • @Aska Remove the line starting with `text`. The last two lines will find the minimum distance and also the points – Santhan Salai Apr 29 '15 at 11:19
  • Thanks once again. I have removed text lines and now only nodes are appearing on the image. Please help me in finding min distance between CH and gateway nodes. Please read my question once more. I am not able to merge your code with mine. :) – Aska Apr 29 '15 at 11:55
  • @Aska, i have trouble with structures. i'm not good at that. I don't understand your code. Get help from from some one who are good at that. – Santhan Salai Apr 30 '15 at 05:51