www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SeparateOneVariableintoThreeVariablesExample.m

    %% Separate One Variable into Three Variables  
% Create a table indicating the amount of snowfall in various towns for
% various storms. 
Storm = [3;3;1;3;1;1;4;2;4;2;4;2];
Town = {'T1';'T3';'T1';'T2';'T2';'T3';...
    'T2';'T1';'T3';'T3';'T1';'T2'};
Snowfall = [0;3;5;5;9;10;12;13;15;16;17;21];

S = table(Storm,Town,Snowfall) 

%%
% |S| contains three snowfall entries for each storm, one for each town. |S| is
% in stacked format.

%% 
% Separate the variable |Snowfall| into three variables, one for each town
% specified in the variable, |Town|. The output table, |U|, is in unstacked format. 
U = unstack(S,'Snowfall','Town') 

%%
% Each row in |U| contains data from rows in |S| that have the same value
% in the grouping variable, |Storm|. The order of the unique values in |Storm|
% determines the order of the data in |U|.