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

    %% Reduce Memory Requirements  
% This example shows how to compare the memory required to store data as
% a cell array of character vectors versus a categorical array. Categorical arrays
% have categories that are defined as character vectors, which can be costly to store
% and manipulate in a cell array of character vectors or |char| array. Categorical
% arrays store only one copy of each category name, often reducing the amount
% of memory required to store the array.   

%% 
% Create a sample cell array of character vectors. 
state = [repmat({'MA'},25,1);repmat({'NY'},25,1);...
    repmat({'CA'},50,1);...
    repmat({'MA'},25,1);repmat({'NY'},25,1)];  

%% 
% Display information about the variable |state|. 
whos state 

%%
% The variable |state| is a cell array of character vectors requiring 17,400 bytes
% of memory.  

%% 
% Convert |state| to a categorical array. 
state = categorical(state);  

%% 
% Display the discrete categories in the variable |state|. 
categories(state) 

%%
% |state| contains 150 elements, but only three distinct categories.  

%% 
% Display information about the variable |state|. 
whos state 

%%
% There is a significant reduction in the memory required to store the variable.