Finding out the mesh indices of randomly generated points
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to find out where does each point from the randomly generated list belong in the pre-defined mesh grid of x and y. The grid is simple as shown on the left of the figure below. The red numbers are the indicies of each cell. The green values on the right are randomly geenrated and I want to find out the index of each point in that mesh.
The attempted code I have is as follows:
close all;
clear all;
%build the mesh area
%x axis
xmin = 0;
xmax = 2;
ndx = 4;
x = linspace(xmin,xmax,ndx+1);
%y axis
ymin = 0;
ymax = 1;
ndy = 2;
y = linspace(ymin,ymax,ndy+1);
%---------------
%Calculate the size of the mesh
N = ndx;
M = ndy;
NCells = N * M;
%---------------
%get the indices of each cell of the xy mesh
for i = 1:N %go through all x subdivisions
for j = 1:M %go throuhg all y subdivisions
%Index matrix of all cells in the mesh
MeshCellInd(i,j)= M*(i-1)+j;
end
end
%---------------
%define xy random points
%generate 10 random numbers on x on the interval [0 2]
% In general, you can generate N random numbers in the interval (a,b)
% with the formula r = a + (b-a).*rand(N,1).
X = 0 + (2-0).*rand(10,1);
%generate 10 random number on y on the interval [0 1]
Y = rand(10,1); %returns a random scalar drawn from the uniform distribution in the interval (0,1).
%concatenate random points on both axes
DATA = [X Y];
%---------------
%find out the index of each x,y randomly generated point
%---------------
Any help would be appreicted.
Thanks.
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!