# How to perform PCA in Matlab when number of dimensions is larger than number of observations?

I have a data matrix of say, $3000 \times 200$, i.e. I have $3000$-dimensional observations from $200$ subjects. How can I reduce the dimensionality to $1000$ in MATLAB?

With bigger numbers, princomp() function causes an out of memory error.

I tried princomp(data, 'econ'); but it only returns a $199$-by-$199$ matrix, so am I limited to the number of observations?

I would appreciate an example of how to get the reduced data, if possible.

• You cannot reduce to 1000 PC dimensions because physically your data set of 200 points and 3000 axes is only 200-dimensional (or 199-dimensional, if you center it). Dec 17, 2014 at 18:53
• Hello, @halilpazarlama! I see that none of your questions have accepted answers. Let me say that you are encouraged to upvote any answer that you find useful (as a way of saying thank you), and also to "accept" (by clicking on a green tick to the left of an answer) an answer that you think answers your question satisfactorily. Accepting gives extra points to the answerer and also generally marks question as resolved. No pressure of course, but e.g. this question seems to be answered and settled by now (and some other of your questions as well). Failing to upvote or to accept is impolite. Dec 21, 2014 at 22:35

First, function princomp is deprecated, you should use pca instead.

Second, as @ttnphns said, if you only have $200$ subjects, then there is no way you can get $1000$ principal components; maximum number is $199$. See here for an explanation: Why are there only $n-1$ principal components for $n$ data points if the number of dimensions is larger or equal than $n$?

Third, as @Aksakal wrote, Matlab's pca assumes that rows in the data matrix correspond to samples, not variables. So you need to transpose your matrix.

Having all of that in mind, here is the code:

X = randn(3000,200);
[eigenvectors, PCs, eigenvalues] = pca(X');

%// eigenvalues is an array of length 199


It appears that you are not calling pca function properly. Your array's must be X(200,3000), while you provide X(3000,200).

This is from the description of princom function:

the n-by-p data matrix X, and returns the principal component coefficients, also known as loadings. Rows of X correspond to observations, columns to variables.

It seems that you mixed up the rows and the columns of your input matrix.

• This is correct, +1. I provided a bit more detailed explanation (even though this question is actually almost off-topic). Dec 18, 2014 at 14:04