i need to create a matrix from 2 vectors

Hi!
i'm working on project which visualizes the 4G network and i'm having some trouble with the deployment of the 4G base stations. i have two vectors that represent the X and Y axis but i don't know how to get them into a matrix.
ex:
X0 = (0:1000:2000);
Y0 = (0:1000:2000);
i want it to give me a matrix that look like this
0,0
1000,1000
2000,2000
0,1000
1000,0
(the order doesn't matter to me)
i wrote this code that does the trick but it only works for 5 base stations:
meshgrid(X0, Y0);
basestations = [X0(:) Y0(:); min(X0) max(Y0); max(X0) min(Y0)];
the visualization looks a bit like this: (red cross = basestation)
If you come up with a good solution please let me know.
thx!

2 Comments

Based on the diagram the last two rows of your example should have 2000's, not 1000's:
0, 0
1000,1000
2000,2000
0,2000
2000, 0
Is this correct?
yes, it seems to be correct. the order doesn't really matter for the rest of the code.
thx for replying btw!

Sign in to comment.

 Accepted Answer

Is there a reason why you wrote this in your code?
meshgrid(X0, Y0);
See that it does absolutely nothing but waste CPU cycles, since it dumps the result of the meshgrid call into the bit bucket.
Anyway, if you have valid code that works for the case you show, then why are you asking for code that does what you want? Are you asking for similar code that works if X0 and Y0 are longer vectors, producing larger sets of combinations with this same basic behavior?
I will suppose that to be the case. Note that if X0 or Y0 have an even number of elements in them, the grid will not be as symmetrical as you asked for.
X0 = 0:7;
Y0 = 0:6;
[X1,Y1] = meshgrid(X0(1:2:end),Y0(1:2:end));
[X2,Y2] = meshgrid(X0(2:2:end),Y0(2:2:end));
basestations= [[X1(:),Y1(:)];[X2(:),Y2(:)]];
plot(basestations(:,1),basestations(:,2),'o')

2 Comments

Hi!
thx for replying i implemented your code and it seems to be working. i copied a part of a old code so that's why the meshgrid part was there. could you explain what happens in your part of the code?
for those who are interested i'm working on the coverage of the 4G network depending on the positioning of the base stations.
this is what it looks like with 5 base stations.
and this is what it looks like with 13 base stations
Meshgrid creates a rectangular lattice.
What you have is NOT a simple rectangular lattice, but visualize it as a pair of rectangular lattices, interleaved. So you will see the two calls to meshgrid.
It is perhaps easier to visualize what was done if the plot was like this:
plot(X1(:),Y1(:),'bo',X2(:),Y2(:),'r+')
So one lattice is shown as blue o's, the second as red +'s. Look carefully at the difference between the two calls to meshgrid.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 22 May 2016
Edited: Stephen23 on 22 May 2016
Method One is a simple solution, but it only works when the grid has an odd number of rows:
>> [X,Y] = meshgrid([0,1000,2000]);
>> [X(1:2:end);Y(1:2:end)].'
ans =
0 0
0 2000
1000 1000
2000 0
2000 2000
Method Two works for odd and even numbers of rows, and does not throw away data:
>> V = 0:1000:2000;
>> [X1,Y1] = meshgrid(V(1:2:end));
>> [X2,Y2] = meshgrid(V(2:2:end));
>> [[X1(:);X2(:)],[Y1(:);Y2(:)]]
ans =
0 0
0 2000
2000 0
2000 2000
1000 1000
Method Three uses toeplitz to generate indices that select the required values:
>> [X,Y] = meshgrid([0,1000,2000]);
>> idx = 1==toeplitz(mod(1:3,2));
>> [X(idx),Y(idx)]
ans =
0 0
0 2000
1000 1000
2000 0
2000 2000

1 Comment

Hi!
thx for replying! really appreciated. your code did not exactly what i wanted but i learned allot anyway
thx agian

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!