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}

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)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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);

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)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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.
[~, 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/

Sunday, March 13, 2011

Matlab Plot Example Change colors and symbols through a for loop

Also, see my gmail for attached function same subject line.


Matlab. control color (colors in a plot).

Some times you want to be able to set your own colors instead of letting the default be determined. A very nice reference is here:
http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html

A detailed Example:

Here is the symbols all setup:

%2. PLOT
%note:
%1. b = blue
%2. k = black
%For all see:
%http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html
colorcell = {'k', 'b','g', 'r', 'y', 'm', 'c', 'w'};
symbolcell ={'.','o', 'x',  '+', '-','*', ':', '-.'};
%plot hold on:
figure(1)

%plot the first value (and then loop over J)
plot(ValCell{1}, IDCell{1}, symbolcell{1}, 'color', colorcell{1})
xlim([0.5 4.5])
hold on;

for i=2:J
  %plot(ValCell{i}, IDCell{i}, 'o','color', colorcell{i})
  plot(ValCell{i}, IDCell{i}, symbolcell{i},'color', colorcell{i})

end

legend('True =1', 'True = 2', 'True = 3', 'True = 4')
hold off;

Very nice reference

http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html


function [IndexCell ValCell IDCell] = PlotclassMLR(sampleID, y, yhat, J)
%PlotclassMLR = Plot classify multinomial logistic regression data.
%This gives one way to graphically depict the classification accuracy.
%See the doc folder.
%INPUTS:
% *sampleID - n x 1. Gives the corresponding sample ID for the values in y and yhat.
%Note: when you produce the test set, you can keep the original SampleID
% *y - n x 1. The true response values
% *yhat - n x 1. The corresponding predicted response variables,
% *J - positive integer. Number of categories
%OUTPUTS:
% null - a graph is produced. SampleID vs. predicted label; true labels are show
%by different colors.

%1. Make the indices for each of the true labels.
IndexCell = cell(J,1);
ValCell = IndexCell;
IDCell = IndexCell;
for i=1:J

       %1. %Note IndexCell{j} gives the indices in y which correspond to true label = j
       IndexCell{i} = find(y == i);

       %2. ValCell = value Cell - keep now the yhat, but sorted by the true
       %value. Hence, ValCell{1} gives the predicted values for all of the respnose
       %which have true value = 1.
       ValCell{i} = yhat(IndexCell{i});

       %3. Similarly we need to store the SampleID to plot them
       %(in a simple example, just equal to the index, but in general,
       %sample ID can be different).
       IDCell{i} = sampleID(IndexCell{i});

end


%2. PLOT
%note:
%1. b = blue
%2. k = black
%For all see:
%http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html
colorcell = {'k', 'b','g', 'r', 'y', 'm', 'c', 'w'};
symbolcell ={'.','o', 'x',  '+', '-','*', ':', '-.'};
%plot hold on:
figure(1)

%plot the first value (and then loop over J)
plot(ValCell{1}, IDCell{1}, symbolcell{1}, 'color', colorcell{1})
xlim([0.5 4.5])
hold on;

for i=2:J
       %plot(ValCell{i}, IDCell{i}, 'o','color', colorcell{i})
       plot(ValCell{i}, IDCell{i}, symbolcell{i},'color', colorcell{i})

end

legend('True =1', 'True = 2', 'True = 3', 'True = 4')
hold off;

 

Saturday, March 5, 2011

Subfigure. Example of using Subfigure (need to update)

%This entry gives an example of using the subfigure package. Together with the iften to conditionally include graphs. The first figure gives an example without subfigure

%This needs to be updated to find a true subfigure example

%preamble

\usepackage{graphicx, subfigure, ifthen}

%Figures:
%-----------------------------------------------
\newboolean{includefigs}
\setboolean{includefigs}{true} %true = show graphs or false = don't
\newcommand{\condcomment}[2]{\ifthenelse{#1}{#2}{}}
%-----------------------------------------------

%main document

\condcomment{\boolean{includefigs}}{
\begin{figure}[t!]
\begin{center}
\includegraphics[scale=.3]{LHflo.eps}
\includegraphics[scale=.3]{LHmv.eps}
\includegraphics[scale=0.3]{LHhW.eps}
\caption{LH experiment. (L) plot of log $\flo$ versus cycle number ($\log F_j$ vs. $j$) for all 16 replicates of $LH_1$ (in blue) and all 16 replicates of $LH_2$ (in red); (C) the mean and variance (taken over the 15 replicates, which were retained for the data analysis, at each cycle) of log $\flo$ versus cycle number for $LH_1$ (blue) and $LH_2$ (red); (R) plot of $\hat{V}_k$ ($\times 10^7$) versus replicate number for $LH_1$ ($+$) and $LH_2$ ($o$).\label{fig:LH}}
\end{center}
\end{figure}
}