简体   繁体   中英

How to access and standardize cell array elements (Matlab)

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM