How do I store x-y coordinate values in a 2 Dimension array (n x n) with its x-y position ?

53 次查看(过去 30 天)
I have seven x-y coordinates and I would like to store in 2 Dimensional array.
These values are to be plot exactly like in this picture. In an array, the x-y coordinate values can be assign with a number let say 1. 1 is refer back to coordinate 1 (27,10). Another example, 7 holds information for x-y coordinate (50,123).
How i do assign the x-y coordinates in x-y location and store in 2D array like in the picture ?
Thanks
  2 个评论
the cyclist
the cyclist 2023-7-17
What exactly is the input you have? Can you upload the input data? Use the paper clip icon in the INSERT section of the toolbar.
Torsten
Torsten 2023-7-17
编辑:Torsten 2023-7-17
I don't understand what you want to store and how.
What I can say is that a usual numerical 2d-matrix can only store a single scalar in each (row,column)-position.
If you want to store two coordinates in one specified position, you either have to use two numerical arrays (X and Y) or you have to use a cell array.

请先登录,再进行评论。

采纳的回答

Chunru
Chunru 2023-7-18
xy = [27 104;
35 100;
47 102;
25 110;
35 114;
47 109;
50 123];
A = zeros(7, 7); % 5x5 is enough for your case
% You may need to adjuxt the following
xc = 25+(0:6)*5+2.5; % centre of grid
yc = 100+(0:6)*5+2.5;
for i=1:size(xy, 1)
[~, ix] = min(abs(xy(i, 1) - xc));
[~, iy] = min(abs(xy(i, 2) - yc));
A(iy, ix) = i;
end
A
A = 7×7
1 2 0 0 3 0 0 4 0 0 0 6 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  2 个评论
hahsaka dahsra
hahsaka dahsra 2023-7-18
Thank you very much for understanding the objective of the code. That's brilliant. I would to understand why you add 2.5 for the centre of the grid. Thank again.
xc = 25+(0:6)*5+2.5; % centre of grid
yc = 100+(0:6)*5+2.5;
Chunru
Chunru 2023-7-19
The grid 25+(0:6)*5 is 25, 30, 35, ...
The center of grid is 27.5, 32.5, ...
So you need to add 2.5 yo obtain the centre of grid.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by