Divide the 3D feature space into grid and then get their labels

조회 수: 2 (최근 30일)
Aravin
Aravin 2022년 1월 31일
댓글: Turlough Hughes 2022년 2월 1일
Dear all,
I have 3D locations in three vectors, x, y, and z. I want to divide the space into fix numbe of grids lets say 2 x 2 x 2. Lets take example in 2D space. I have two vectors x, and y, as below;
x = [1 1 2 2 3 10];
y = [1 2 1 1 2 5];
plot(x,y, '+')
now I divide in to 2 x 2. So the interval for x would be 0-to-5 and then 5.1-to-10, and likewise for y(0-to-2.5, and 2.6-to-5). I have drawn the points and divided the space manually as shown below.
now the there are four cells. I have vector r that should be r = [3,3,3,3,3,2] where all points are in cell 3 and only point 5,10 is on cell 2.

채택된 답변

Turlough Hughes
Turlough Hughes 2022년 1월 31일
Example in 2D
% Data
x = rand(1,100)*10;
y = rand(1,100)*5;
r = zeros(size(x)); % group index
r(x>0 & x<=5 & y>0 & y<=2.5) = 1;
r(x>5 & x<=10 & y>0 & y<=2.5) = 2;
r(x>0 & x<=5 & y>2.5 & y<=5) = 3;
r(x>5 & x<=10 & y>2.5 & y<=5) = 4;
figure(), gscatter(x,y,r)
hold on, axis padded, legend off
xline(5), yline(2.5)
Here I'm using conditions > as well as <= so that you can select one edge, eg 5 instead of switching between 5 and 5.1 etc, though if you need to modify from this then do so as required.
Example in 3D
% Additional data for z
z = rand(1,100)*5;
% E for Edges
xE = [0 5 10];
yE = [0 2.5 5];
zE = [0 2.5 5]; % You can see why just one value for the edge is useful.
% Group the data according to the edges (r gives the groups).
idx = 1;
for ii = 1:numel(xE)-1
for jj = 1:numel(yE)-1
for kk = 1:numel(zE)-1
r(x>xE(ii) & x<=xE(ii+1) & y>yE(jj) & y<=yE(jj+1) & z>zE(kk) & z<=zE(kk+1)) = idx;
idx = idx + 1;
end
end
end
cmap = jet(8);
figure(), scatter3(x,y,z,80,cmap(r,:),'filled')
Not the best visualisation but you can see that it does the job.
  댓글 수: 6
Aravin
Aravin 2022년 2월 1일
What version of matlab you are using? I don't have xline in 2018.
Turlough Hughes
Turlough Hughes 2022년 2월 1일
The xline() and yline() functions were introduced in 2018b.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by