Showing specific values on x or y axes - matlab

a=[1 2 7 5 4 6]
t=0:2:10
plot(t,a);
I want that in the plot on x axis only values : 0, 2, 4, 6, 8, 10 (as per t=0:2:10 in the code) should be depicted on the time axis unlike the above which shows all values. How do I do this ?

Those are the xticks, set them like this:
set(gca,'Xtick',0:2:10);
If you want to hide the yticks:
set(gca,'Ytick',[]);

Related

How to interpolate this?

I have a problem with interpolating.
the plot of the points is
here.
If we zoom in , the plot looks something like this
this.
I do not know how to use the Matlab interp1 function to interpolate this.
Explanation:
This is a space time diagram, i.e. the x axis is the space and the y-axis is the time.
Hence, the structure of the vectors x and y is as follows:
The vector y is defined as
y=(1,1,2,2,3,3,4,4,...,2500,2500)
and the x-vector contains (pairwise) the positions, i.e.
x(i) and x(i+1) are the different positions at time y(i)=y(i+1).
I think the problem maybe is that at different times, we have the same points as can be seen in the zoomed in picture above.
How many time-steps the x-values remain the same differs, sometimes they remain the same for 3 time-steps, sometimes even for 4 time-steps, sometimes only for 2 time-steps.
You can use acccumarray to perform some operation on the values in Y that correspond to each unique value of X.
Using some example data:
x = [1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8];
y = 1:length(x);
We get something like the following:
Now use accumarray to perform some calculation on each cluster:
clusteravg = accumarray(x', y, [], #mean);
clustermax = accumarray(x', y, [], #max);
clustermin = accumarray(x', y, [], #min);
The first input to accumarray is an array of subscripts, the second input is an array of values that correspond to those subscripts. accumarray collects all of the elements of the second input that share the same subscript (first input) and performs the specified calculation on them. Here I've calculated the average, max, and min for each cluster of values:
Yay

Set color of specific values with colormap

so a small version of what I am trying to accomplish is I have a matrix A;
A = [0 1 0; 2 0 0;1 3 6;9 0 1];
imagesc(A)
So when I use imagesc(A) I get a nice grid with each value represented by a different color. However I want to be able to set the value of 0 specifically to white and ideally be able to change the other colors as I see fit such as if I know two values represent the same thing like 3 and 6, then they could be set to the same or relatively similar colors. Is imagesc the wrong command to use because from what I can tell it uses a color gradient.
Thanks
2 options:
you can create your own colormap as shown in How to create a custom colormap programmatically?
or simply map your matrix A to a matrix that would be coloured as you want it. So if you know you want 3 and 6 to the same colour then create a mapping function that makes that so. You then use A to index the map so the 3rd and 6th element of map must be the same e.g.
map = [1, 2, 3, 4, 5, 6, 4, 7, 8, 9, 10];
imagesc(map(A+1))
note that elements 4 and 7 in map are the same because your A values start from 0, this is also why there is the +1 in the second line.
and then just choose a colormap that starts from white.
Personally I would go with method 1.
Simple solution:
% get colormap and set first value to white
cmap = colormap;
cmap(1,:) = [1 1 1];
% apply new colormap
colormap(cmap);
% display matrix
imagesc(A);
Obviously you can change the colors for other values in the same way

How to draw two box plots horizontally and at the same height in one figure?

I have two 1x5 vectors as follows.
A=[1 2 3 4 5]
B=[7 8 9 10 11]
Note that their entry values have no overlapping at all. I wish to have two box plots that lie horizontally at the same height of the y-axis, i.e. side by side, in one figure.
I learn that
x = rand(5,1);
y = rand(10,1);
z = rand(15,1);
group = [repmat({'First'}, 5, 1); repmat({'Second'}, 10, 1); repmat({'Third'}, 15, 1)];
boxplot([x;y;z], group, 'orientation', 'horizontal')
is able to draw three box plots together in one figure. However, the three box plots lie on three different y-axis "levels".
How may I do this?
Use the position property:
A = [1 2 3 4 5];
B = [7 8 9 10 11];
group = [repmat({'First'}, 5, 1); repmat({'Second'}, 5, 1)];
boxplot([A';B'], group, 'orientation', 'horizontal','positions',[1 1])
As you can see, the y-labels are screwed up and manual labeling is required.
... which is quite easy to solve by just substituting {'Second'} with {' '}. You could use annotations instead for manual labeling.
group = [repmat({'boxplots'}, 5, 1); repmat({' '}, 5, 1)];
boxplot([A';B'], group, 'orientation', 'horizontal','positions',[1 1])
annotation('textbox', [0.35,0.8,0.07,0.06],...
'String', 'First','LineStyle','none');
annotation('textbox', [0.7,0.8,0.11,0.06],...
'String', 'Second','LineStyle','none');

Matlab 3D plot plotting weird lines?

I have 3 arrays X, Y, Z that look something like this:
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
z = [1, 2, 3, 2, 4, 6, 3, 6, 9]
Then I am plotting these points with plot3(x,y,z).
However the result is something unexpected.. It's basically meant to appear like a log-graph, it does this but it also has extra lines. I have no idea how it's happening, maybe someone here can enlighten me!
Here is an example of my graph (obviously I've plotted my values for X, Y, Z and the arrays above are just an example of what they look like):
As you can see, the bottom curved lines are the log-graph lines which I am happy to be seeing, but the one that appears above it confuse me. I tried displaying the X Y Z values that match the following pattern: X is between 110 and 120 and Y = 0.05. This should theoretically give me TWO matches for each X. ie. Between X = [110, 120], I should get 20 matches since the graph shows two lines hitting at the point Y = 0.05 for each X.
To make it more clear what values of X are being graphed, it's basically increments of 0.1 so between X = 120 and X = 119, it is plotting 119.0, 119.1, ... , 120.0. In any case, only two points of X, Z hit Y = 0.05 between 119-120.
As I was saying, I checked for the values of X, Z that matched Y = 0.05 and it only returns ONE result per X. That is, for X = [110, 120], only 10 matches returned. These values were values on the bottom lines (ie. the log-graph lines) and NOT the top line. So effectively, these lines are not meant to exist. They are definitely hitting Y = 0.05 and are between X = [110, 120] so they should be appearing as a match..
So there you have it. Not really sure what's going on!
If someone could help, that'd be great.
EDIT - More Info.
Even manually putting the X, Y values to the function which returns Z never reaches the values on the upper line.
Thanks.
The example for x, y and z actually illustrates your problem quite well.
You are not plotting individual curves (one for each log-plot) but rather one continuous curve. Hence your extra lines are in fact connecting one end of a log curve to the beginning of the next.
You have to break up your plot command.
Edit
In the end it will require knowledge of your dataset, but for the x, y, z you provided above, this will work to produce three independent curves instead of one:
x = [1, 1, 1, 2, 2, 2, 3, 3, 3];
y = [1, 2, 3, 1, 2, 3, 1, 2, 3];
z = [1, 2, 3, 2, 4, 6, 3, 6, 9];
DATASET_SIZE = 3;
hold on;
for i=1:size(x,2)/DATASET_SIZE
plot3(x((i-1)*DATASET_SIZE+1:i*DATASET_SIZE),y((i-1)*DATASET_SIZE+1:i*DATASET_SIZE),z((i-1)*DATASET_SIZE+1:i*DATASET_SIZE));
plot3(x(i*DATASET_SIZE+1:2*DATASET_SIZE),y(i*DATASET_SIZE+1:2*DATASET_SIZE),z(i*DATASET_SIZE+1:2*DATASET_SIZE));
plot3(x(2*DATASET_SIZE+1:3*DATASET_SIZE),y(2*DATASET_SIZE+1:3*DATASET_SIZE),z(2*DATASET_SIZE+1:3*DATASET_SIZE));
view(3)
end
Edit 2
An easy way of finding out if this "problem" occured, is using the Data Cursor in the MATLAB plot window. Place it anywhere on your graph and use the up or down arrow keys to move around. If you can move along the entire length of the plot, it is a continuous line. Furthermore, you should see the Cursor jump from the "end" of one curve to the "start" of the next.
I had the same problem using surface(x,y,z) and addressed the issue by changing the renderer. MATLAB has 3 different renderers for plotting data and by default the openGL renderer plots as you have shown. You can view hwich renderer your figure uses with GET(gcf). You can change renderer as I have done using: set(gcf,'Renderer','painters')
The 3rd renderer I believe is zbuffer and may work as well.

Repeating sequences in a single matlab plot

Say I have 2 vectors:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
b = [1, 3, 5, 7, 9, 7, 5, 3, 1];
I want to plot these vectors against each other, a being X, and b being Y.
However, while I want the plotted point to be in the correct location, I want the actual values shown on the X axis to be a function of the values in a, where the result of the function is repeated over a given period AND I want these values to be sequential along the axis.
For example, say the function for the value to show on the X axis is mod(a - 1, 3) + 1. I would like for the X axis to read something like 1, 2, 3, 1, 2, 3, 1, 2, 3 as in
I have a feeling that some combination of subplot and axes may be required, but I'm not seeing anything obvious in the documentation.
You can set this via the axes properties xtick and xticklabel.
set(gca, 'xtick', a, 'xticklabel', mod(a,3)+1);
This won't give you the ticks you've described, but mod(a-1,3)+1 will. I'm not sure if you wanted the function you gave or the picture you showed, though.
Try setting the XtickLabels, like so,
>> set(gca,'XTickLabel',{'1','2','3'})