"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?"

6 次查看(过去 30 天)
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

回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-7-26
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 个评论
Sonia Goyal
Sonia Goyal 2019-7-26
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 2019-7-26
编辑:KALYAN ACHARJYA 2019-7-26
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!

请先登录,再进行评论。


Guillaume
Guillaume 2019-7-26
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));

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by