0

So I need to build a matrix of x and y coordinates. I have the x stored in one matrix called vx=0:6000; and y stored in Vy=repmat(300,1,6000);.

Values in x are 0,1,2,...,5999,6000. Values in y are 300,300,...,300,300.

How do I build a "vector" with the x,y coordinates above?

It would look like this [(0,300);(1,300);...;(5999,300);(6000,300)].

After I finish doing this, I am going to want to find the distance between another fixed point x,y (that I will replicate 6000 times) and the vector above, in order to make a distance graph over time.

Thank you so much!

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
belgarion
  • 115
  • 1
  • 11

2 Answers2

4

You can just use horizontal concatenation with []

X = [Vx(:), Vy(:)];

If you want to compute the distance between another point and every point in this 2D array, you could do the following:

point = [10, 100];
distances = sqrt(sum(bsxfun(@minus, X, point).^2, 2));

If you have R2016b or newer you can simply do

distances = sqrt(sum((X - point).^2, 2));
Suever
  • 64,497
  • 14
  • 82
  • 101
3

A slightly more elegant alternative (in my opinion) is the following:

Vx = (0:1:6000).';
C = [Vx 0*Vx+300];  % Just a trick to avoid the overly verbose `repmat`.

p = [10,100];       % Define some point of reference.
d = pdist2(C,p);    % The default "distance type" is 'euclidian' - which is what you need.

This uses the pdist2 function, introduced in MATLAB 2010a, and requires the Statistics and Machine Learning Toolbox.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99