Clear Filters
Clear Filters

Storing a nodal points in cells based on the coordinates of the points

6 views (last 30 days)
Having a set of nodal points with (x,y) coordinates. The goal is to store all the points in cells (cell 1, cell 2,...etc) variable based on their coordinates as shown in the figure below. The cells are numbered row by row as shown in figure.
The two variables below are required:
1- cellToNode{cellNo}(NoOfNodesInThisCell,1) : a Matlab cell that has all the nodes in each cell
2- nodeToCell(nodeNo,1) : a Matlab vector that has the cell no. containing a specific node
For example, When run in Matlab console: >> cellToNode{1}
The output is all nodes in cell 1
ans =
3
7
9
11
And when run, for exmaple: nodeToCell(1)
ans =
4
Since cell no. 1 has 4 nodes
Note: The set of nodes and cells in figure is just an example and the the real number of the points is large.

Answers (1)

Shubham Dhanda
Shubham Dhanda on 14 Jun 2023
Hi, 
I understand that you want to display number of nodal points in each cell using nodeToCell variable and cell number of each nodal point in using cellToNode variable. From the information provided by you, I am assuming the nodal points and Grid Size.
nodalPoints = [3 1; 1 2; 2 2; 3 2; 2 3; 3 3; 1 4; 3 4; 2 5; 3 5];
gridSize = [3 3];
Below is the MATLAB code implementation of the problem:
% Define nodal points and grid size
nodalPoints = [3 1; 1 2; 2 2; 3 2; 2 3; 3 3; 1 4; 3 4; 2 5; 3 5];
gridSize = [3 3];
% Initialize variables
cellToNode = cell(prod(gridSize), 1);
nodeToCell = zeros(size(nodalPoints, 1), 1);
% Iterate over nodal points
for i = 1:size(nodalPoints, 1)
x = nodalPoints(i, 1);
y = nodalPoints(i, 2);
% Determine which cell the nodal point belongs to
cellX = ceil(x / gridSize(1));
cellY = ceil(y / gridSize(2));
cellNo = (cellY - 1) * gridSize(1) + cellX;
% Add nodal point to cell
cellToNode{cellNo} = [cellToNode{cellNo}; i];
% Update nodeToCell variable
nodeToCell(i) = cellNo;
end
% Display the nodal points in each cell
for i = 1:prod(gridSize)
fprintf('Nodes in cell %d:\n', i);
disp(cellToNode{i}');
end
Nodes in cell 1:
1 2 3 4 5 6
Nodes in cell 2: Nodes in cell 3: Nodes in cell 4:
7 8 9 10
Nodes in cell 5: Nodes in cell 6: Nodes in cell 7: Nodes in cell 8: Nodes in cell 9:
% Display the cell number for each nodal point
for i = 1:size(nodalPoints, 1)
fprintf('Node %d in cell %d\n', i, nodeToCell(i));
end
Node 1 in cell 1 Node 2 in cell 1 Node 3 in cell 1 Node 4 in cell 1 Node 5 in cell 1 Node 6 in cell 1 Node 7 in cell 4 Node 8 in cell 4 Node 9 in cell 4 Node 10 in cell 4
  2 Comments
Dhafer Jadaan
Dhafer Jadaan on 19 Jun 2023
Thanks Shubham. You did not store the points based on their coordinates in each cell as I mentioned.
Shubham Dhanda
Shubham Dhanda on 25 Jun 2023
Hi Dhafer,
I have used sample data to solve the problem, you can change the nodalPoints based on your need to get the answer that you need. The idea was to tell you how you can solve your problem and I think you got it.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!