MATLAB Answers

How to group and then 3D plot a column in a matrix?

3 views (last 30 days)
Suppletory
Suppletory on 20 Jul 2017
Edited: Carl on 24 Jul 2017
Dear All,
I have matrix M that is 2500x4 like so (I put in the headings myself):
Color X Y Z
42 1.006431 1.144329 92.1
43 1.006436 1.122343 61.1
42 1.006553 1.023647 54.1
33 1.006755 1.754235 73.2
47 1.006043 1.234565 87.6
39 1.006324 1.622433 99.5
... ... ... ...
I would like to plot the Color column as a 3D plot with X Y and Z as my coordinate system. Each point will vary according to the value of Color. However, since there are many variables, it may be smarter to put all the common X, Y, Z values into groups and averaging the Color in each group. X and Y can be 5 significant figures each and Z can be 2 significant figures. For example, at (1.0064,1.1443,92), the average color will be some value around 42. If you are confused please let me know. Thank you for your time.

  0 Comments

Sign in to comment.

Answers (1)

Carl
Carl on 24 Jul 2017
Edited: Carl on 24 Jul 2017
You can use the scatter3 function and specify the color for each one of your points. See the quick example below:
c = zeros(6,3);
data = [(1:6)', (1:6)', rand(6,1)];
% red if z greater than 0.5
c(data(:,3) > 0.5, 1) = 1;
% blue if z less than or equal to 0.5
c(data(:,3) <= 0.5, 3) = 1;
scatter3(data(:,1),data(:,2),data(:,3),[],c,'filled')
See the documentation for more details on using the scatter3 function:
Specifically, you can supply the value of c as a "three-column matrix of RGB triplets" as shown above, or define your own colormap and index into it.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by