File Exchange

image thumbnail

pivottable

version 1.0.0.0 (2.44 KB) by Zhiqiang Zhang
PIVOTTABLE Groups a matrix into a pivot table, is flexible as in Excel

3.2K Downloads

Updated 27 Feb 2011

View License

out = pivottable(inMatrix, pivotRow, pivotColumn, valueColumn, valueFun) group
the input matrix "inMatrix" into a pivottable similar with Excel. Anyone
who are familiar with the pivottable in Excel will not have difficulties
with this function.

How the pivottables created are shown in following examples:
let
inMatrix = {
'2009', 'Mon', 12, 31;
'2009', 'Wed', 11, 34;
'2009', 'Fri', 1, 4;
'2009', 'Mon', 3, 4;
'2009', 'Wed', 9, 6;
'2009', 'Fri', 1, 4;
'2010', 'Mon', 18, 15;
'2010', 'Wed', 11, 21;
'2010', 'Wed', 1, 4;
};
then
pivottable(inMatrix, 1, 2, 3, @sum) output a cell
[] 'Fri' 'Mon' 'Wed'
'2009' [ 2] [ 15] [ 20]
'2010' [] [ 18] [ 12]
pivottable(inMatrix, [1 2], [], 3, @sum) output a cell
'2009' 'Fri' [ 2]
'2009' 'Mon' [15]
'2009' 'Wed' [20]
'2010' 'Mon' [18]
'2010' 'Wed' [12]
pivottable(inMatrix, [], 2, 3, @sum) output a cell
'Fri' 'Mon' 'Wed'
[ 2] [ 33] [ 32]
pivottable(inMatrix, 1, 2, [3 4], {@sum, @(x)(numel(x))})
[] 'Fri' 'Fri' 'Mon' 'Mon' 'Wed' 'Wed'
'2009' [ 2] [ 2] [ 15] [ 2] [ 20] [ 2]
'2010' [] [] [ 18] [ 1] [ 12] [ 2]

author: zhang@zhiqiang.org, http://zhiqiang.org/blog/it/pivottable-in-matlab.html
version: 2011-02-28 ver 1

Cite As

Zhiqiang Zhang (2021). pivottable (https://www.mathworks.com/matlabcentral/fileexchange/30547-pivottable), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: mjeppesen/matlab-pivot-table, mat2piv.m

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!