0

I have a simple plot question.

On x axis, the values are K, say from 2 to 12, discrete. On y axis, the values are C, say from 1 to 10, discrete.

My function is piecewise:

K if K<2C; K+2C if K>=2C;

I want to show the values at points (K,C):

(1,1) Show as 1 (1,2) Show as 1 (2,1) Show as 4 (2,2) Show as 2 ect.

How would I do that?

Many thanks,

Casper

1 Answers1

0

You can use ndgrid to create K and C:

[K C] = ndgrid(2:12,1:10);

then use logical indexing to calculate the separate parts:

z=zeros(11,10);
ind = K>=(2*C);
z(~ind) = K(~ind);
z(ind) = K(ind)+2*C(ind);

then plot any way you want:

surf(C,K,z);

or

image(z);

and others....

pseudoDust
  • 1,336
  • 1
  • 10
  • 18
  • Thanks for the answer. However, that's not exactly what I want. I am hoping to get exactly what I described. A black and white 2D plot. Plain and simple. X axis - K, from 2 to 12, left to right; Y axia - C, from 1 to 10, bottom to top; then at each (K,C) coordinate: (1,1) Show the value 1 (1,2) Show the value 1 (2,1) Show the value 4 (2,2) Show the value 2 ect. – Chen Stats Yu Sep 28 '13 at 22:51
  • What do you mean black and white 2d plot? i.e. when you say Show the value 1 how is that value represented? you have "3D data" here, K coordinate, C coordinate, and value, you can create a heat map (something like [this](http://i.stack.imgur.com/spyXr.gif), only with different shades of gray for the different values). is that what you want? – pseudoDust Sep 29 '13 at 07:52
  • Sorry if I wasnt clear enough. Yes, I mean a b/w 2D plot. You can think of it as a "sketch". For example, at the coordinate: (1,1), a numeric value 1 is printed, (1,2) the value 1 is printed, ect. I know what a heat map is, but it's not what i am after. Thanks. – Chen Stats Yu Sep 29 '13 at 16:12
  • Perhaps this will give you a better idea. All your Z values are correct. I want to add text at (K,C) the 'string' Z. text(K,C,'Z') for all K from 2 to 12 and C from 1 to 10. Is there an efficient (proper) way to do it? The problem with 'text' is that the 'Z' has to be strings and the position are slightly off. It's not exactly 'on' the point (K,C) sharp. – Chen Stats Yu Sep 29 '13 at 22:36
  • Essentially, you want a table... double click Z in the variable window... or, try [this](http://www.mathworks.com/matlabcentral/fileexchange/15877-heat-maps-with-text) – pseudoDust Sep 30 '13 at 09:22