"how to create a matrix of coordinates and how to calculate the distance b/w different coordinates of the same matrix w.r.t the any single coordinate element?"

2 views (last 30 days)
I want to create a matrix as given below for any input, and suppose the input is "4".
a = (-10.0) (0,0) (10,0) (20,0)
(-10,5) (0,5) (10,5) (20,5)
(-10,10) (0,10) (10,10) (20,10)
(-15,10) (0,15) (10,15) (20,15)
But i dont know how to do and also i want to calculate the distance of a single element say (0,0) w.r. t the rest of the elements?
Please help me with this problem

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 26 Jul 2019
You may look at cell array
a={[-10,0],[0,0],[10,0],[20,0];[-10,5],[0,5],[10,5],[20,5];[-10,10],[0,10],[10,10],[20,10];[-15,10],[0,15],[10,15],[20,15]};
x1=[0,0]; % Single element as you said
distance_result=zeros(1,16);
for i=1:16
x2=a{i};
data1=x2(1)-x1(1);
data2=x2(2)-x1(2);
distance_result(i)=sqrt(data1^2+data2^2);
end
distance_result
Commad window:
>> whos a
Name Size Bytes Class Attributes
a 4x4 2048 cell
Result:
distance_result =
Columns 1 through 12
10.0000 11.1803 14.1421 18.0278 0 5.0000 10.0000 15.0000 10.0000 11.1803 14.1421 18.0278
Columns 13 through 16
20.0000 20.6155 22.3607 25.0000
Hope it helps!
  2 Comments
Sonia Goyal
Sonia Goyal on 26 Jul 2019
Thank you for your response.
But is there any way to generate the above said matrix instead of entering it manually in cell array??
And also, i have to calculate the distance in matrix form and the expected output shall be in matrix too,
KALYAN ACHARJYA
KALYAN ACHARJYA on 26 Jul 2019
Edited: KALYAN ACHARJYA on 26 Jul 2019
But is there any way to generate the above said matrix instead of entering it manually in cell array??
sorry, I didnot find any relation of inputs to create that mat.
And also, i have to calculate the distance in matrix form and the expected output shall be in matrix too,
a={[-10,0],[0,0],[10,0],[20,0];[-10,5],[0,5],[10,5],[20,5];[-10,10],[0,10],[10,10],[20,10];[-15,10],[0,15],[10,15],[20,15]};
x1=[0,0]; % Single element as you said
distance_result=zeros(4,4);
for i=1:4
for j=1:4
x2=a{i,j};
data1=x2(1)-x1(1);
data2=x2(2)-x1(2);
distance_result(i,j)=sqrt(data1^2+data2^2);
end
end
distance_result
Result:
distance_result =
10 0 10 20
11.18 5 11.18 20.616
14.142 10 14.142 22.361
18.028 15 18.028 25
Note: If you could avoid multiple loops, it would be better.
Good Luck!

Sign in to comment.


Guillaume
Guillaume on 26 Jul 2019
It's not clear where your X and Y come from and it's also not clear how you want the points to be stored since you don't use valid matlab syntax for your example
%maybe
X = -10:10:20;
Y = 0:5:15;
%create cartesian product of X and Y
[Xall, Yall] = ndgrid(X, Y);
points = [Xall(:), Yall(:)];
Here I've stored them as Nx2 matrix with column 1 the X, and column 2 the Y.
To calculate the distance of all points from another point:
source = [5, 7]; %whatever you want. A 1x2 vector
distances = sqrt(sum((points - source).^2, 2);
or
source = [5, 7];
distances = hypot(points(:, 1) - source(1), points(:, 2) - source(2));

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!