From: Oleg Komarov on
"Tom De Temmerman" <tom_dt5(a)hotmail.com> wrote in message <hl1dhl$atc$1(a)fred.mathworks.com>...
> Hi guys,
> the problem is the following
>
> i have a table of this format (m by 3 matrix):
> X Y count
> 1 1 3
> 1 4 1
> 1 5 6
> 3 4 2
> 5 2 1
> 5 3 9
> 6 2 2
> ...
>
> the table is sorted first by X then by Y. Count is the frequency of X appearing in Y.
>
> and i want to turn this into the following matrix:
> Y
> 1 2 3 4 5 ...
> X 1 3 0 0 1 6
> 3 0 0 0 2 0
> 5 0 1 9 0 0
> 6 0 2 0 0 0
> ...
> for each X you now have only 1 row
>
> i'm an absolute beginner when it comes to matlab and I don&#8217;t know if this is even possible, so any help is much appreciated.
>
> regards

% Using http://www.mathworks.com/matlabcentral/fileexchange/26119-pivotunpivot

m=[1 1 3
1 4 1
1 5 6
3 4 2
5 2 1
5 3 9
6 2 2];

padWith = 0;
fun = [];
removeHeaders = false;

Out = Pivot(m,fun,removeHeaders,padWith).';
Out(1) = 0;
Out =
0 1 2 3 4 5
1 3 0 0 1 6
3 0 0 0 2 0
5 0 1 9 0 0
6 0 2 0 0 0

You can play removing headers, padding with NaNs (default beahvior)...
Out = Pivot(m,[],true).';
Out(1) = 0
Out =

0 NaN NaN 1.00 6.00
NaN NaN NaN 2.00 NaN
NaN 1.00 9.00 NaN NaN
NaN 2.00 NaN NaN NaN

Oleg