Thursday, October 16, 2008

Plotting 5 D data

% our 5-dimensional data
data = rand(7, 5);
n = size(data, 1);

% precompute sphere data
[xs, ys, zs] = sphere(25);

% open a figure
figure(1);
clf;
hold on;
view(3);

% initialize the axis
grid on;
axis equal;

% colormap resolution and colormap type
ncolors = 1024;
colormap = jet(ncolors);

% convert 5th column to indexes into the colormap
coloridx = data(:,5);
coloridx = coloridx - min(coloridx);
coloridx = coloridx / (max(coloridx) - min(coloridx));
coloridx = 1 + ceil((ncolors - 1) * coloridx);

% extract the colors we need from the colormap
colors = colormap(coloridx,:);

for i = 1 : n

% get radius and color of this sphere
radius = data(i,4) / 5;
color = colors(i,:);

% adjust the sphere's x-y-z-coordinates and radius
xsi = xs * radius + data(i,1);
ysi = ys * radius + data(i,2);
zsi = zs * radius + data(i,3);

% plot the sphere
surf(xsi, ysi, zsi, ...
'EdgeColor', 'none', ...
'FaceColor', color, ...
'BackFaceLighting', 'lit');
end

% set up a nice light
light;
lighting phong

% grab'n'rotate
rotate3d on