i need to create a matrix from 2 vectors
Show older comments
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
Stephen23
on 22 May 2016
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?
Runar van Geffen
on 22 May 2016
Accepted Answer
More Answers (1)
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
Runar van Geffen
on 22 May 2016
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


