1. randp - probably the best
http://www.mathworks.com/matlabcentral/fileexchange/8891
RANDP - pick random values with relative probability
R = RANDP(PROB,..) returns integers in the range from 1 to
NUMEL(PROB) with a relative probability, so that the value X is
present approximately (PROB(X)./sum(PROB)) times in the matrix R.
2. there is this on the FEX - easier to use not as efficient
actually I often use this one for ease.
http://www.mathworks.com/matlabcentral/fileexchange/21912-sampling-from-a-discrete-distribution
sampling from a discrete distribution
The use of this function is easy:
x = discretesample(p, n).
You just input the probability mass, and tell the function how many sample you want to sample, then it returns the samples in form of a 1 x n vector.
3. what about this entry.
[~,x] = histc(rand(1,n),[0;cumsum(p(:))/sum(p)]);
Thursday, April 28, 2011
Wednesday, March 30, 2011
latex Table. A multicolumn example
I give a simple example and a more complicated one that needs to use sideways table (because it is too big).
%Example 0. See here
http://andrewjpage.com/index.php?/archives/43-Multirow-and-multicolumn-spanning-with-latex-tables.html
%Example A.
\begin{table}
\caption{Post-treatment Egg Count Varability (Simulated Data). We postulate that the data on a
single farm contains $n=13$ samples from the fixed $p$ model. This table presents such samples at three instances for horses in Table \ref{tbl:prevar} with $p=.95$.\label{tbl:postvar}} %postvar = varab ility in post-treatment count
%we want Hello centered so use c
\begin{center}
\begin{tabular}{cc|ccc}
& & \multicolumn{3}{|c|}{Hello}\\
horse & pre & post (sim 1) & post (sim 2) & post (sim 3) \\ \hline
1 & 3250 & 163 & 167 & 163 \\
2 & 450 & 28 & 26 & 26 \\
3 & 445 & 25 & 21 & 21 \\
4 & 300 & 20 & 17 & 14 \\
5 & 260 & 16 & 9 & 16 \\
6 & 1420 & 73 & 65 & 78 \\
7 & 505 & 20 & 20 & 27 \\
8 & 45 & 4 & 4 & 3 \\
9 & 1230 & 70 & 60 & 59 \\
10 & 325 & 12 & 19 & 14 \\
11 & 325 & 18 & 9 & 22 \\
12 & 100 & 2 & 1 & 2 \\
13 & 80 & 4 & 1 & 1
\end{tabular}
\end{center}
\end{table}
%Example B.
\usepackage{rotate}
You can get away without sideways table here
\begin{table}
\caption{Post-treatment Egg Count Varability (Simulated Data). We postulate that the data on a
single farm contains $n=13$ samples from the fixed $p$ model. This table presents such samples at three instances for horses in Table \ref{tbl:prevar} with $p=.95$.\label{tbl:postvar}} %postvar = varab ility in post-treatment count
%we want Hello centered so use c
\begin{center}
\begin{tabular}{cc|ccc|ccc|}
& & \multicolumn{3}{|c|}{Hello} & \multicolumn{3}{|c|}{Hello}\\
horse & pre & post (sim 1) & post (sim 2) & post (sim 3) & post (sim 1) & post (sim 2) & post (sim 3) \\ \hline
1 & 3250 & 163 & 167 & 163 & 168 & 210 & 93\\
\end{tabular}
\end{center}
\end{table}
%Example 0. See here
http://andrewjpage.com/index.php?/archives/43-Multirow-and-multicolumn-spanning-with-latex-tables.html
%Example A.
\begin{table}
\caption{Post-treatment Egg Count Varability (Simulated Data). We postulate that the data on a
single farm contains $n=13$ samples from the fixed $p$ model. This table presents such samples at three instances for horses in Table \ref{tbl:prevar} with $p=.95$.\label{tbl:postvar}} %postvar = varab ility in post-treatment count
%we want Hello centered so use c
\begin{center}
\begin{tabular}{cc|ccc}
& & \multicolumn{3}{|c|}{Hello}\\
horse & pre & post (sim 1) & post (sim 2) & post (sim 3) \\ \hline
1 & 3250 & 163 & 167 & 163 \\
2 & 450 & 28 & 26 & 26 \\
3 & 445 & 25 & 21 & 21 \\
4 & 300 & 20 & 17 & 14 \\
5 & 260 & 16 & 9 & 16 \\
6 & 1420 & 73 & 65 & 78 \\
7 & 505 & 20 & 20 & 27 \\
8 & 45 & 4 & 4 & 3 \\
9 & 1230 & 70 & 60 & 59 \\
10 & 325 & 12 & 19 & 14 \\
11 & 325 & 18 & 9 & 22 \\
12 & 100 & 2 & 1 & 2 \\
13 & 80 & 4 & 1 & 1
\end{tabular}
\end{center}
\end{table}
%Example B.
\usepackage{rotate}
You can get away without sideways table here
\begin{table}
\caption{Post-treatment Egg Count Varability (Simulated Data). We postulate that the data on a
single farm contains $n=13$ samples from the fixed $p$ model. This table presents such samples at three instances for horses in Table \ref{tbl:prevar} with $p=.95$.\label{tbl:postvar}} %postvar = varab ility in post-treatment count
%we want Hello centered so use c
\begin{center}
\begin{tabular}{cc|ccc|ccc|}
& & \multicolumn{3}{|c|}{Hello} & \multicolumn{3}{|c|}{Hello}\\
horse & pre & post (sim 1) & post (sim 2) & post (sim 3) & post (sim 1) & post (sim 2) & post (sim 3) \\ \hline
1 & 3250 & 163 & 167 & 163 & 168 & 210 & 93\\
\end{tabular}
\end{center}
\end{table}
latex Table Example Rotated (sideways?)
An example where you need to present the table in landscape for it to fit
\usepackage{rotating}\begin{sidewaystable}
\centering
\begin{tabular}{|llllllllp{1in}lp{1in}|}
\hline
Context &Length &Breadth/ &Depth &Profile &Pottery &Flint &Animal &Stone &Other &C14 Dates \\
& &Diameter & & & & &
Bones&&&\\
\hline
&&&&&&&&&&\\
\multicolumn{10}{|l}{\bf Grooved Ware}&\\
784 &--- &0.90m &0.18m &Sloping U &P1 &$\times$46 & $\times$8 &&$\times$2 bone& 2150$\pm$ 100 BC\\
785 &--- &1.00m &0.12 &Sloping U &P2--4 &$\times$23 & $\times$21 & Hammerstone &---&---\\
962 &--- &1.37m &0.20m &Sloping U &P5--6 &$\times$48 & $\times$57* & ---& ---&1990 $\pm$ 80 BC (Layer 4) 1870 $\pm$90 BC (Layer 1)\\
983 &0.83m &0.73m &0.25m &Stepped U &--- &$\times$18 & $\times$8 & ---& Fired clay&---\\
&&&&&&&&&&\\
\multicolumn{10}{|l}{\bf Beaker}&\\
552 &--- &0.68m &0.12m &Saucer &P7--14 &--- & --- & --- &--- &---\\
790 &--- &0.60m &0.25m &U &P15 &$\times$12 & --- & Quartzite-lump&--- &---\\
794 &2.89m &0.75m &0.25m &Irreg. &P16 $\times$3 & --- & --- &--- &---\\
\hline
\end{tabular}
\caption[Grooved Ware and Beaker Features, their Finds and Radiocarbon
Dates]{Grooved Ware and Beaker Features, their Finds and Radiocarbon
Dates; For a breakdown of the Pottery Assemblages see Tables I and
III; for the Flints see Tables II and IV; for the Animal Bones see
Table V.}\label{rotfloat2} \end{sidewaystable}
Matlab Plot - Simple example with labeled axes
Matlab Plot - Simple example with labeled axes and changing other options.
Figure example
%GraphScriptfinal
%use thickline and label y,x
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
load 'matdata.mat'
% whos
% A 60x1 480 double
% B 30x1 240 double
% C 20x1 160 double
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Note we can force the support:
%%%%%%%%%%%%%%%%%%%%%
% x = [randn(30,1); 5+randn(30,1)];
% xi = linspace(-10,15,201);
% f = ksdensity(x,xi,'function','cdf');
%%%%%%%%%%%%%%%%%%%%%
% %II. Graph - Combined DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %standard call:
% [f,xi] = ksdensity(x, 'support', [0,1]);
% plot(xi,f);
%Put all 64 Farms together
figure(1)
[fA,xiA] = ksdensity(A, 'support', 'positive');
[fB,xiB] = ksdensity(B, 'support', 'positive');
[fC,xiC] = ksdensity(C, 'support', 'positive');
% plot(xiA,fA)
% hold on
% plot(xiB,fB)
% hold on
% plot(xiC,fC)
% legend('1-avg','2-avg','3-avg')
linenu = 2;
plot(xiA,fA,xiB,fB,xiC,fC, 'LineWidth',linenu);
legend('1-avg','2-avg','3-avg')
xlabel('Egg Count', 'fontsize',14)
ylabel('Probability Density', 'fontsize',14)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Figure example
%GraphScriptfinal
%use thickline and label y,x
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
load 'matdata.mat'
% whos
% A 60x1 480 double
% B 30x1 240 double
% C 20x1 160 double
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Note we can force the support:
%%%%%%%%%%%%%%%%%%%%%
% x = [randn(30,1); 5+randn(30,1)];
% xi = linspace(-10,15,201);
% f = ksdensity(x,xi,'function','cdf');
%%%%%%%%%%%%%%%%%%%%%
% %II. Graph - Combined DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %standard call:
% [f,xi] = ksdensity(x, 'support', [0,1]);
% plot(xi,f);
%Put all 64 Farms together
figure(1)
[fA,xiA] = ksdensity(A, 'support', 'positive');
[fB,xiB] = ksdensity(B, 'support', 'positive');
[fC,xiC] = ksdensity(C, 'support', 'positive');
% plot(xiA,fA)
% hold on
% plot(xiB,fB)
% hold on
% plot(xiC,fC)
% legend('1-avg','2-avg','3-avg')
linenu = 2;
plot(xiA,fA,xiB,fB,xiC,fC, 'LineWidth',linenu);
legend('1-avg','2-avg','3-avg')
xlabel('Egg Count', 'fontsize',14)
ylabel('Probability Density', 'fontsize',14)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Matlab Subplot Example Histograms
This is a subplot example with histograms
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
load RayData
%A 110x4 3520 double
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%{
%1. horse 1
i=1;
figure(i)
%now plot
hist(A(:,1))
%2. use X in other ones
i=2;
figure(i)
hist(A(:,2))
%3. Figure 3
i=3;
figure(i)
hist(A(:,3))
%4. Figure 4
i=4;
figure(i)
hist(A(:,4))
%}
%Part II. Plot in single subplot
%-----------------------------------------
%Basic Example
%{
figure;
subplot(1,2,1); plot(count(:))
subplot(1,2,2); hist(count(:),5)
datacursormode on
top row (left to right)
>> subplot(2,2,1)
>> subplot(2,2,2)
bottom row (left to right)
>> subplot(2,2,3)
>> subplot(2,2,4)
%}
figure(1);
subplot(2,2,1); hist(A(:,1)); xlabel('Horse 1', 'fontsize', 16);
subplot(2,2,2); hist(A(:,2)); xlabel('Horse 2', 'fontsize', 16);
subplot(2,2,3); hist(A(:,3)); xlabel('Horse 3', 'fontsize', 16);
subplot(2,2,4); hist(A(:,4)); xlabel('Horse 4', 'fontsize', 16);
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
load RayData
%A 110x4 3520 double
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%{
%1. horse 1
i=1;
figure(i)
%now plot
hist(A(:,1))
%2. use X in other ones
i=2;
figure(i)
hist(A(:,2))
%3. Figure 3
i=3;
figure(i)
hist(A(:,3))
%4. Figure 4
i=4;
figure(i)
hist(A(:,4))
%}
%Part II. Plot in single subplot
%-----------------------------------------
%Basic Example
%{
figure;
subplot(1,2,1); plot(count(:))
subplot(1,2,2); hist(count(:),5)
datacursormode on
top row (left to right)
>> subplot(2,2,1)
>> subplot(2,2,2)
bottom row (left to right)
>> subplot(2,2,3)
>> subplot(2,2,4)
%}
figure(1);
subplot(2,2,1); hist(A(:,1)); xlabel('Horse 1', 'fontsize', 16);
subplot(2,2,2); hist(A(:,2)); xlabel('Horse 2', 'fontsize', 16);
subplot(2,2,3); hist(A(:,3)); xlabel('Horse 3', 'fontsize', 16);
subplot(2,2,4); hist(A(:,4)); xlabel('Horse 4', 'fontsize', 16);
Matlab Plot: Multiple KDEs
Below is a script for plotting kernel density estimates (KDEs) in Matlab
%ScriptGraphCombo
%subplot, putting the two on the same graph.
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
%load in the data;
load data/NB25.mat
%work with
%X 5000x5 200000 double
%Y 5000x5 200000 double
linenu = 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Note we can force the support:
%%%%%%%%%%%%%%%%%%%%%
% x = [randn(30,1); 5+randn(30,1)];
% xi = linspace(-10,15,201);
% f = ksdensity(x,xi,'function','cdf');
%%%%%%%%%%%%%%%%%%%%%
%II. Graph - Combined DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%this is if you simply use a normal density - keep away from zero
ep=.01;
Y = max(Y,ep);
[fA,xA] = ksdensity(Y(:,1));
[fB,xB] = ksdensity(Y(:,2));
[fC,xC] = ksdensity(Y(:,3));
[fD,xD] = ksdensity(Y(:,4));
[fE,xE] = ksdensity(Y(:,5));
xvector=[xA;xB;xC;xD;xE]';
fvector=[fA; fB; fC; fD; fE]';
plot(xvector, fvector, 'LineWidth',linenu);
legend('avg-1','avg-2', 'avg-3', 'avg-4', 'avg-5')
xlabel('Egg Count', 'fontsize',14)
ylabel('Probability Density', 'fontsize',14)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ScriptGraphCombo
%subplot, putting the two on the same graph.
%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
%load in the data;
load data/NB25.mat
%work with
%X 5000x5 200000 double
%Y 5000x5 200000 double
linenu = 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Note we can force the support:
%%%%%%%%%%%%%%%%%%%%%
% x = [randn(30,1); 5+randn(30,1)];
% xi = linspace(-10,15,201);
% f = ksdensity(x,xi,'function','cdf');
%%%%%%%%%%%%%%%%%%%%%
%II. Graph - Combined DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%this is if you simply use a normal density - keep away from zero
ep=.01;
Y = max(Y,ep);
[fA,xA] = ksdensity(Y(:,1));
[fB,xB] = ksdensity(Y(:,2));
[fC,xC] = ksdensity(Y(:,3));
[fD,xD] = ksdensity(Y(:,4));
[fE,xE] = ksdensity(Y(:,5));
xvector=[xA;xB;xC;xD;xE]';
fvector=[fA; fB; fC; fD; fE]';
plot(xvector, fvector, 'LineWidth',linenu);
legend('avg-1','avg-2', 'avg-3', 'avg-4', 'avg-5')
xlabel('Egg Count', 'fontsize',14)
ylabel('Probability Density', 'fontsize',14)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Saturday, March 26, 2011
Matlab: Sort Arrays (e.g. sort by a given column)
I can also choose to sort all of A by a particular column. First sort that column, and then use those indices to sort the entire array.
There is a very nice entry here:
http://blogs.mathworks.com/loren/2010/02/04/constrained-sorting-of-array/
[~, index2] = sort(A(:,2)); Asort2 = A(index2,:)
There is a very nice entry here:
http://blogs.mathworks.com/loren/2010/02/04/constrained-sorting-of-array/
Subscribe to:
Posts (Atom)