surface plot of cell arrays

Hi,
I am wondering if it's possible to (at least mimic) surf/surc functions with cell arrays with each element having a different length (i.e. not a simple rectangular matrix). I am most interested in the color coding of values so that a 2D view of the plot conveys the magnitude of the function plotted.
Cheers

 Réponse acceptée

Moes
Moes le 15 Juin 2011

0 votes

Bugger!
My aim in using cell arrays is to minimize number of calculations done. Here are simplified versions of what I have and what I want:
% Code I have (computationally intensive)
divs_x = 10;
D_x = linspace(1, divs_x, divs_x);
divs_y = 5*max(D_x);
D_y = linspace(0, pi, divs_y);
z = zeros(divs_x, divs_y);
for k = 1 : divs_x,
z(k, :) = cos(D_y);
end;
surf(D_y, D_x, z);
colormap(hot(128));
colorbar;
% Code I want but cannot plot
divs_x = 10;
D_x = linspace(1, divs_x, divs_x);
z = cell(1, divs_x);
D_y = cell(1, divs_x);
for k = 1 : divs_x,
divs_y = 5*D_x(k);
D_y{k} = linspace(0, pi, divs_y);
z{k} = cos(D_y{k});
end;
I could use plot3 to plot individual cells but it won't produce the color mapping that I want. I am not terribly concerned with filled surfaces (but would be nice).

6 commentaires

Moes
Moes le 15 Juin 2011
One way to plot the second piece of code is to use:
figure;
hold on;
for i = 1 : divs_x,
plot3(D_x(i)*ones(1,length(D_y{i})), D_y{i}, z{i});
end;
but this does not do color coded value of function z.
Walter Roberson
Walter Roberson le 15 Juin 2011
I had misunderstood, I think. I thought that your surface was defined over a rectangle but you wanted that tiled irregularly. But it appears that instead your surface is not defined over a rectangle ?
If you initialize a rectangular array with either NaN or inf, then you can fill in just the parts that are defined. surf() will interpret NaN or inf as meaning not to plot anything. For technical reasons, inf is faster to work with than NaN is.
Moes
Moes le 15 Juin 2011
Ah excellent! I'll give it a try.
Moes
Moes le 15 Juin 2011
I tried the following code, but it does not produce the correct result:
divs_x = 10;
D_x = linspace(1, divs_x, divs_x);
z = inf(divs_x, 5*max(D_x));
D_y = inf(divs_x, 5*max(D_x));
for k = 1 : divs_x,
divs_y = 5*D_x(k);
D_y(k,1:divs_y) = linspace(0, pi, divs_y);
z(k,1:divs_y) = cos(D_y(k,1:divs_y));
end;
figure;
surf(D_y(end,:), D_x, z)
The following code produces the right graph but I do not know how to apply a color map to it:
divs_x = 10;
D_x = linspace(1, divs_x, divs_x);
z = cell(1, divs_x);
D_y = cell(1, divs_x);
for k = 1 : divs_x,
divs_y = 5*D_x(k);
D_y{k} = linspace(0, pi, divs_y);
z{k} = cos(D_y{k});
end;
figure;
hold on;
for i = 1 : divs_x,
scatter3(D_x(i)*ones(1,length(D_y{i})), D_y{i}, z{i});
end;
Walter Roberson
Walter Roberson le 15 Juin 2011
pointsize = 8;
scatter3(D_x(i)*ones(1,length(D_y{i})), D_y{i}, z{i}, pointsize, z{i});
Moes
Moes le 15 Juin 2011
Ha! Fantastic.
Changing the pointsize to something a bit larger and also having it "filled" produces a very much acceptable plot when the stepsize in D_x and D_y domains are small. Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 15 Juin 2011

1 vote

Not with cell arrays. You could use patch() though.
On the other hand, I am not sure why you don't use
surf(X,Y,Z,Z)
as that would use the Z value to interpolate the Color

Community Treasure Hunt

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

Start Hunting!

Translated by