1

I was thinking of a question similar to this.

I have a function which takes as input the three values x,y,z from the R^3 and returns either 1,2,3,4. Now I wanted to plot the point in the 3D space with coordinates (x,y,z) with a color associated with the functional value at that point which can be either one of 1,2,3 or 4.

I have a 3D matrix with integer entries like say 1,2,3,4 and I store the points value in this matrix so that I can plot the points with the corresponding color (similar trick of 'image' command in MATLAB for making 2D plots).

color coding (say)-

1 - green, 2 - blue , 3 - cyan , 4 -red

Like if at the point (0.5,0.5,0.1) the function returns the value 3, then I mark the point (0.5,0.5,0.1) with the color associated to number three which is cyan.

I am thinking of a MATLAB command which does this in the case of three dimensional case as the "image" command seems to work for the 2D case.

obchardon
  • 10,614
  • 1
  • 17
  • 33
BAYMAX
  • 115
  • 7
  • 1
    Related: [Produce a 3D stem plot with a custom colormap in MATLAB](https://stackoverflow.com/q/29349336/2545927) – kkuilla Mar 20 '19 at 11:04

2 Answers2

2

I can only think of some kind of workaround, like this:

% Input: A = coordinates, b = functional values.
A = rand(20, 3);
b = ceil(rand(20, 1) * 4);

% Color map.
cm = [0 1 0; 0 0 1; 0 1 1; 1 0 0];

% Circle size.
cs = 21;

% 3D scatter plot.
figure(1);
hold on;
for k = 1:size(cm, 1)
  idx = (b == k);
  scatter3(A(idx, 1), A(idx, 2), A(idx, 3), cs, cm(k, :), 'filled');
end
hold off;
view(45, 30);
grid on;

Gives the following output:

Output

HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • Nice!! thanks!, I was wondering that if 'idx' becomes zero when $b \neq k$, then there will be an error regarding $A(idx,1)$? as we are trying to access $A(0,1)$? – BAYMAX Mar 20 '19 at 20:37
  • Also sems like idx can have only 0 or 1? if so then how are we going to access other remaining rows of A? – BAYMAX Mar 20 '19 at 20:39
  • `idx` is a vector of "logicals", here indicating which rows of `A` should be addressed. Have a look at [Indexing with Logical Values](https://de.mathworks.com/help/matlab/math/array-indexing.html#d120e1410). – HansHirse Mar 20 '19 at 20:46
  • Exactly! so idx can have values as 0 or 1, so when we try t oaccess say 5th row of A then if idx is 1 then it acccesses the first row and when idx is zero it accesses which row? as idx takes two possile values 0 or 1, so how does it access al lrows of A when we do A(idx,1) ? as idx is a vector which has values 0 or 1 – BAYMAX Mar 20 '19 at 21:17
  • @BAYMAX In the example, `A` has 20 rows (x-y-z values). Also, `idx` has 20 rows (logical values, not `0` and `1` like integers as you may have in mind). So, for each row in `idx` where the value is `true`, the corresponding row in `A` is adressed. For example, let `idx = [1 1 1 1 1 0 ... 0]`, then `A(idx, :)` would give the first five rows of `A`. I don't know, how to explain that in another way. That's how indexing in Matlab works. – HansHirse Mar 20 '19 at 21:24
2

You can linearize the solution suggested by @HansHirse, so a small improvement could be:

% Dummy data
A = rand(20, 3);
b = ceil(rand(20, 1) * 4);
% color vector
c = [0 1 0; 0 0 1; 0 1 1; 1 0 0];
% Use the linear indexing to select the right color
scatter3(A(:,1),A(:,2),A(:,3),[],c(b,:),"filled")

Even simpler you can just use b as color input and matlab will use the default colormap to set the color according to b

scatter3(A(:,1),A(:,2),A(:,3),[],b,"filled")
obchardon
  • 10,614
  • 1
  • 17
  • 33