Subtract mean from each table by columns

6 visualizaciones (últimos 30 días)
wbadry
wbadry el 14 de Jun. de 2020
Comentada: Image Analyst el 22 de Jun. de 2020
Hello,
Assume we have the following table
t = array2table([rand(10,1),rand(10,1),rand(10,1)],'VariableNames',{'feat_1', 'feat_2','feat_3'});
t =
10×3 table
feat_1 feat_2 feat_3
________ ________ _________
0.12991 0.60198 0.82582
0.56882 0.26297 0.53834
0.46939 0.65408 0.99613
0.011902 0.68921 0.078176
0.33712 0.74815 0.44268
0.16218 0.45054 0.10665
0.79428 0.083821 0.9619
0.31122 0.22898 0.0046342
0.52853 0.91334 0.77491
0.16565 0.15238 0.8173
I can get the mean and standard deviation for each solumn using
meanArray = mean(table2array(t,1));
stdArray = std(table2array(t,1));
meanArray =
0.3479 0.4785 0.5547
stdArray =
0.2415 0.2836 0.3793
Is there any vector way to caclulate (element - mean / std) on every element in table without loop and keep the table? Thanks
I know normalize could standardize data but I need to make it in code.

Respuesta aceptada

Image Analyst
Image Analyst el 14 de Jun. de 2020
Editada: Image Analyst el 14 de Jun. de 2020
Try this:
t = array2table([rand(10,1),rand(10,1),rand(10,1)],'VariableNames',{'feat_1', 'feat_2','feat_3'})
m = table2array(t);
meanArray = mean(m)
stdArray = std(m)
z = (m - meanArray) ./ stdArray % a matrix
zt = array2table(z, 'VariableNames',{'feat_1', 'feat_2','feat_3'}) % a table

Más respuestas (1)

wbadry
wbadry el 22 de Jun. de 2020
Hello,
I came by another solution using bsxfun
Here is a complete example based on my question
% Generate random columns for the example (10 by 3)
t = array2table([rand(10,1),rand(10,1),rand(10,1)],'VariableNames',...
{'feat_1', 'feat_2','feat_3'});
% Subtract from the mean vector (3 by 1)
t1 = bsxfun(@minus, mean(table2array(t)),table2array(t));
% Divide by stadard deviation vector (3 by 1) using right array division
t2 = bsxfun(@rdivide,t1,std(table2array(t)));
% Create the standardized table
t = array2table(t2,'VariableNames',{'feat_1', 'feat_2','feat_3'});
% Check mean and std of feat_1
feat_1_mu = mean(t.feat_1)
feat_1_sig = std(t.feat_1)
feat_1_mu =
0
feat_1_sig =
1
  1 comentario
Image Analyst
Image Analyst el 22 de Jun. de 2020
Yeah, you can do it that way if you want. I always find their *xfun() functions very cryptic and confusing than the regular math symbols. Plus you always have to find or figure out what the operations are. I mean, who would know, off the top of their head, that if you want to divide something you use @rdivide instead of the slash symbol /, or @minus instead of -. And it gets even more confusing for other operations.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by