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

4 views (last 30 days)

Show older comments

Aravin on 31 Jan 2022

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels

Commented: Turlough Hughes on 1 Feb 2022

Accepted Answer: Turlough Hughes

Open in MATLAB Online

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.

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

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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Turlough Hughes on 31 Jan 2022

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#answer_885465

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#answer_885465

Open in MATLAB Online

Example in 2D

% Data

x = rand(1,100)*10;

y = rand(1,100)*5;

r = zeros(size(x)); % group index

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)

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

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')

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

Not the best visualisation but you can see that it does the job.

6 Comments

Show 4 older commentsHide 4 older comments

Aravin on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964075

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964075

Thank you Huges. Wonderfull. As I stated, the grid size which we have kept 2 x 2 or 2 x 2 x 2 is just an example. Could you please help in making n x n or n x n x n ? As I have to experiment with different values of n.

Your support is highly appreciated on this.

Turlough Hughes on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964170

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964170

Open in MATLAB Online

Just use linspace to define your edges, for example:

% Data

x = rand(1,1000)*10;

y = rand(1,1000)*5;

z = rand(1,1000)*5;

r = zeros(size(x)); % group index

% E for Edges

n = 3;

xE = linspace(min(x)-1e-6, max(x), n+1);

yE = linspace(min(y)-1e-6, max(y), n+1);

zE = linspace(min(z)-1e-6, max(z), n+1);

% Group the data according to the edges

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 = colorcube(n^3);

figure(), scatter3(x,y,z,200,cmap(r,:),'filled')

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

Aravin on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964180

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964180

Thank you so much. Please illusarate in 2D too. sorry for being naive.

Turlough Hughes on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964200

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964200

Open in MATLAB Online

% Data

x = rand(1,500)*10;

y = rand(1,500)*5;

r = zeros(size(x)); % group index

% E for Edges

n = 6;

xE = linspace(min(x)-1e-6, max(x), n+1);

yE = linspace(min(y)-1e-6, max(y), n+1);

% Group the data according to the edges

idx = 1;

for ii = 1:numel(xE)-1

for jj = 1:numel(yE)-1

r(x>xE(ii) & x<=xE(ii+1) & y>yE(jj) & y<=yE(jj+1)) = idx;

idx = idx + 1;

end

end

cmap = colorcube(n^2);

figure(), scatter(x,y,100,cmap(r,:),'filled')

xline(xE)

yline(yE)

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

Aravin on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964280

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964280

What version of matlab you are using? I don't have xline in 2018.

Turlough Hughes on 1 Feb 2022

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964300

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1639770-divide-the-3d-feature-space-into-grid-and-then-get-their-labels#comment_1964300

The xline() and yline() functions were introduced in 2018b.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D Plots

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

  • 2d
  • 3d
  • handles

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


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

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Divide the 3D feature space into grid and then get their labels (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5754

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.