Storing a nodal points in cells based on the coordinates of the points
5 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
% 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
2 Comments
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!