Reputation: 393
I have a cell array data structure and I want to standardize the elements of it. How can I apply the zscore
or the mean and std to each cell array? I tried doing the following but it is incorrect. The code below randomly generates 50 different sets of data each of 1000 elements. I even tried the following cellfun(@(x)(x-mu)./sg,X,'UniformOutput',false)
but I cannot compute the mu
(mean) and sg
(standard deviation) for the cell structure. Please help.
for n = 1: 50
x=rand(1,1000);
X(n)= {x} ;
end
XV = zscore(X);
Upvotes: 1
Views: 432
Reputation: 60494
zscore
is a function in the Statistics Toolbox, which I don't have. The documentation says that you can apply it to standardize a matrix using the 'all'
option (note! this is new to MATLAB R2018b):
zscore(X,0,'all'); % won't work in versions of MATLAB before R2018b
If, like me, you don't have this toolbox, it should be equivalent to:
(X - mean(X(:))) / std(X(:));
So we can write an anonymous function like this:
standardize = @(X)(X - mean(X(:))) / std(X(:));
or equivalently
standardize = @(X)zscore(X,0,'all');
Now, you can apply this function to all matrices in an array using cellfun
:
X = cell(5,3);
for ii=1:numel(X)
X{ii} = rand(1,1000);
end
XV = cellfun(standardize,X,'UniformOutput',false);
Upvotes: 1