Finding a zero meaned matrix vertically and horizontally.
11 views (last 30 days)
Show older comments
I am trying to understand the first principles of PCA and was set with an exercise that requires me to create a zero meaned matrix in the direction of the features (columns) and variables (rows).For example,for meaning rows,I just need to calculate each rows mean and subtract it in every single elements in the row.However I also need the column mean to be zero simultanously.How can I achieve this ? Applying the same method for columns then rows does not work as it shifts the rows means after they have been zeroed.
0 Comments
Accepted Answer
John D'Errico
on 11 Feb 2022
Edited: John D'Errico
on 11 Feb 2022
If you are writing a PCA code for yourself, then yes, you want to mean subtract the array. (If you are just going to use the existing PCA tool, then no, there is no need to mean subtract the data.) But only along one of the dimensions, the rows. For example, I've attached the famous iris data set to this answer so that we can use it in an example.
load irisdata
whos
irismean = mean(irisdata,1)
So every column mean has been computed.
irissub = irisdata - irismean; % the mean subtracted array. So each column has had the mean of that column subtracted off.
I think you may have been confused, because here I am talking about rows and columns at once. You compute the mean of the columns, and the result is naturally a row vector. Now subtract off that row of means, from EVERY row in the matrix. I did that in computing irissub.
However, you do not need both the row AND column means to be simultaneously zero to use PCA. Are you saying that you were told to do that? I think you were mistaken. Now you can use SVD to do the heavy lifting.
[U,S,V] = svd(irissub,0);
diag(S)
V
I'll let you go from there, since it appears you must be wanting to write the PCA yourself.
0 Comments
More Answers (1)
Image Analyst
on 11 Feb 2022
Not sure you need to do that. The documentation for pca() says "By default, pca centers the data and uses the singular value decomposition (SVD) algorithm." so it's done internally for you.
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!