log sacle colorbar: how to set ticks? - matlab

I want to set the ticks of the colorbar to be in log scale, with simple readable ticks: 2 3 4 5 6 7 8 9 10 20 30 (and not just "10^0", "10^1");
for example I do:
x = linspace(0,0.9);
y=logspace(-1,1);
[X,Y] = meshgrid(x,y);
Z = 220 *sqrt((1-X).*Y); %just random function(x,y)
[M,c]= contourf(X,Y,Z,100);
c.LineColor = 'none';
set(gca,'ColorScale','log')
cl=colorbar;
ylabel(cl, 'color')
cl.Ticks=[ 2 3 4 5 6 7 8 9 10 20 30];
cl.TickLabels = num2cell([ 2 3 4 5 6 7 8 9 10 20 30]);
It doesn't work that way. How to do it?

Your code works well and as expected. If you check your colorbar, the lower limit value is 22, and therefore you only see the last label.
x = linspace(0,0.9);
y=logspace(-1,1);
[X,Y] = meshgrid(x,y);
Z = 220 *sqrt((1-X).*Y); %just random function(x,y)
[M,c]= contourf(X,Y,Z,100);
c.LineColor = 'none';
set(gca,'ColorScale','log')
cl=colorbar;
ylabel(cl, 'color')
cl.Limits=[2 cl.Limits(2)] % change the range
cl.Ticks=[ 2 3 4 5 6 7 8 9 10 20 30];
cl.TickLabels = num2cell([ 2 3 4 5 6 7 8 9 10 20 30]);

Related

Matlab - Finding values within a matrix

How can I find the row that have all the values from A into the matrix B and display the index of the rows using Matlab?
A= [2 5 6];
B=[1 2 4 9 10 15 27 30;
1 2 3 4 5 6 7 8;
1 2 3 5 6 9 22 101;
2 4 5 6 14 20 22 23]
Thanks
With bsxfun in 3D -
ind = find(all(any(bsxfun(#eq,B,permute(A,[1 3 2])),2),3))
With bsxfun again, but keeping it in 2D -
ind = find(sum(reshape(any(bsxfun(#eq,B(:),A(:).'),2),size(B)),2)==numel(A))
With ismember -
ind = find(sum(reshape(ismember(B(:),A(:)),size(B)),2)==numel(A))
With pdist2 from Statistics and Machine Learning Toolbox -
ind = find(sum(reshape(any(pdist2(B(:),A(:))==0,2),size(B)),2)==numel(A))
With knnsearch again from Statistics and Machine Learning Toolbox-
[~,dists] = knnsearch(A(:),B(:))
ind = find(sum(reshape(dists==0,size(B)),2)==numel(A))
Sample run -
A =
2 5 6
B =
1 2 4 9 10 15 27 30
1 2 3 4 5 6 7 8
1 2 3 5 6 9 22 101
2 4 5 6 14 20 22 23
ind =
2
3
4

Quiver from a single matrix

I have a matrix (In Matlab) which I need to plot with a quiver plot but I don't know how to turn it into a "quiver plot-able form". Are there any commands to change it? I saw some example on MathWorks Homepage using the peaks function but I couldn't get it to work.
The matrix I have is pretty huge with lots of NaN's so I created an smaller version of it.
Temperature = [ 1 2 2 2 3 4 6 7 ;
1 2 3 4 4 5 6 7 ;
2 3 4 NaN NaN 6 8 9 ;
3 4 5 NaN NaN 7 8 9 ;
4 4 6 6 7 8 10 11;
4 5 7 7 8 9 11 12];
contour(Temperature)
%quiver(Temperature)
In the comments you find the example code from the documentation which I used. Besides knowing that the dimensions are ordered [Y,X,Z] to get a matching meshgrid it's mostly copy&paste
% figure
figure
% [X,Y] = meshgrid(-2:.2:2);
Temperature = [ 1 2 2 2 3 4 6 7 ;
1 2 3 4 4 5 6 7 ;
2 3 4 NaN NaN 6 8 9 ;
3 4 5 NaN NaN 7 8 9 ;
4 4 6 6 7 8 10 11;
4 5 7 7 8 9 11 12];
[X,Y] = meshgrid(1:size(Temperature,2),1:size(Temperature,1));
% Z = X.*exp(-X.^2 - Y.^2);
Z=Temperature;
% [DX,DY] = gradient(Z,.2,.2);
[DX,DY] = gradient(Z,1,1);
% contour(X,Y,Z)
contour(X,Y,Z)
% hold on
hold on
% quiver(X,Y,DX,DY)
quiverscaling=3;
quiver(X,Y,DX,DY,quiverscaling)
% colormap hsv
colormap hsv
% hold off
hold off

plot two histograms (using the same y-axis) and a line plot (using a different y-axis) on the same figure

How can I plot two histograms (using the same y-axis) and a line plot (using a different y-axis) on the same figure? I am using Matlab 2014b. I am aware of this but it seems to only work for bar plots?
This is my histogram code:
A = [1 2 2 2 3 4 5 5 5 5 5 5 5 5 5 6 6 6 7 7];
B = [6 6 6 7 7 7 7 7 7 7 8 8 8 9 9 10 10];
hist(A,7);
hold on
hist(B,7);
h = findobj(gca,'Type','patch');
set(h(1),'FaceColor','b','EdgeColor','b','facealpha',0.2)
set(h(2),'FaceColor','r','EdgeColor','r','facealpha',0.2)
xlabel('Day','fontsize',14)
ylabel('Frequency','fontsize',14)
xlim([1 10])
Now say I have these data:
Day = [1 2 3 4 5 6 7 8 9 10];
Prevalence = [3 2 4 8 5 6 7 8 9 5];
I want to plot these data (plot(Day,Prevalence)) using the right y-axis.
Thanks.
I think this workaround will do what you want.
Basically create a new axes at the same position than the one in which the histograms are plot, however set its color property to 'none' and the YAxisLocation to the right. You can then assign the new axes the properties you want.
Code:
clear
clc
%// ====================
%// Your code
A = [1 2 2 2 3 4 5 5 5 5 5 5 5 5 5 6 6 6 7 7];
B = [6 6 6 7 7 7 7 7 7 7 8 8 8 9 9 10 10];
hist(A,7);
hold on
hist(B,7);
h = findobj(gca,'Type','patch');
set(h(1),'FaceColor','b','EdgeColor','b','facealpha',0.2)
set(h(2),'FaceColor','r','EdgeColor','r','facealpha',0.2)
xlabel('Day','fontsize',14)
ylabel('Frequency','fontsize',14)
xlim([1 10])
%// ====================
Day = [1 2 3 4 5 6 7 8 9 10];
Prevalence = [3 2 4 8 5 6 7 8 9 5];
%// Get the current axes position to place the new one.
AxesPos = get(gca,'Position');
hold on
hax2 = axes('Position',AxesPos);
%// Plot the data
plot(Day,Prevalence,'--k','LineWidth',4,'Parent',hax2)
%// Set properties of the axes.
set(hax2,'Color','none','YAxisLocation','right','XTick',[],'XTickLabel','','YLim',[0 15])
ylabel('Prevalence','FontSize',16)
%// Rotate the label to correct orientation
LabelPos = get(get(hax2,'YLabel'),'Position');
set(get(hax2,'YLabel'),'Position',[LabelPos(1)+.2 LabelPos(2) LabelPos(3)],'Rotation',-90)
Output:
Note that it's far from perfect ...for example the left border of the first axes is not visible...that could be fixed by playing around with the position of the new axes. Hopefully it does the job for you!

How can i store results from a loop in a matrix?

I have the following loop
x = [1 2 3 4 5;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6]
y=[10 30 24 35 40]'
one=[]
for i=1:5
a=i;
ind=[a]
one=x(:,[i])
[b_LS, sigma_b_LS, s_LS] = lscov(one,y)
s = s_LS
aicx1=size(one,1)*log(s)+2*size(one,2)
end
I want to store result as :
A=[ind;aicx1] for example A=[1 2 3 4 5; 26 34 24 325]
You could add at the end of the loop:
x = [1 2 3 4 5; 4 5 6 8 9; 8 7 6 3 1; 5 6 7 9 1; 6 4 2 9 6];
y = [10 30 24 35 40]';
one=[];
for ii=1:5
one = x(:,ii);
[b_LS, sigma_b_LS, s_LS] = lscov(one,y);
s = s_LS;
aicx1 = size(one,1) * log(s) + 2 * size(one,2);
%% Add this
A(1,ii) = ii;
A(2,ii) = aicx1;
end
Notes
Avoid using i or j as variables since they are used for complex numbers
Add an ; at the end of the sentence if you don't want/need the values to appear in the command window

Plot 3d surface plot in matlab/freemat

I would like to ask about 3d surface plotting. As I am new to it, I was just trying out. Basically, I have 3 parameters, x, y ,z which I have the values from experimental datas and I would like to plot them out. As such, i tried,
x= [6 7 8 9 10 11 12 1]
x =
6 7 8 9 10 11 12 1
--> y=[2 3 4 5 6 1 6 8]
y =
2 3 4 5 6 1 6 8
--> z= [3 4 5 6 7 8 9 10]
z =
3 4 5 6 7 8 9 10
meshgrid(x,y,z)
surf(x,y,z)
The plot window did come out but there was no graph. Is my method wrong?
Thanks!
It sounds like you need to start with plot3, as you're just describing a set of points in 3D, rather than points on a mesh or surface. See if that does what you want.
x = [6 7 8 9 10 11 12 1];
y = [2 3 4 5 6 1 6 8];
z = [3 4 5 6 7 8 9 10];
plot3(x, y, z, '.');
This is how I would plot a surface :
%define the data
x=[6 7 8 9 10 11 12 1 6 7 8 9 10 11 12 1];
y=[2 3 4 5 6 1 6 8 2 3 4 5 6 1 6 8];
z=[3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10];
%Create 3D surface
[X,Y]=meshgrid(x,y);
Z=griddata(x,y,z,X,Y);
%Plot the surface
surface(X,Y,Z);
shading interp %makes it look sexy
%xlim([])
%ylim([])
Sometimes I use axis limets to make the plot look nicer (eliminates the unneeded white area's); for this set of data I could use xlim([6 11]) and ylim([2 6]).