machinery
machinery

Reputation: 6290

Reducing dimensionality of features with PCA in MATLAB

I'm totally confused regarding PCA. I have a 4D image of size 90x60x12x350. That means that each voxel is a vector of size 350 (time series).

Now I divide the 3D image (90x60x12) into cubes. So let's say a cube contains n voxels, so I have n vectors of size 350. I want to reduce this n vectors to only one vector and then calculate the correlations between all vectors of all cubes.

So for a cube I can construct the matrix M where I just put each voxel after each other, i.e. M = [v1 v2 v3 ... vn] and each v is of size 350.

Now I can apply PCA in Matlab by using [coeff, score, latent, ~, explained] = pca(M); and taking the first component. And now my confusion begins.

  1. Should I transpose the matrix M, i.e. PCA(M')?

  2. Should I take the first column of coeff or of score?

  3. This third question is now a bit unrelated. Let's assume we have a matrix A = rand(30,100) where the rows are the datapoints and the columns are the features. Now I want to reduce the dimensionality of the feature vectors but keeping all data points.

    How can I do this with PCA?

    When I do [coeff, score, latent, ~, explained] = pca(M); then coeff is of dimension 100x29 and score is of size 30x29. I'm totally confused.

Upvotes: 1

Views: 5884

Answers (2)

noobie2023
noobie2023

Reputation: 783

I disagree with the answer above.

[coeff,score]=pca(A)

where A has rows as observations and column as features.

If A has 3 featuers and >3 observations (Let's say 100) and you want the "feature" of 2 dimensions, say matrix B (the size of B is 100X2). What you should do is:

B = score(:,1:2);

Upvotes: 0

rlbond
rlbond

Reputation: 67847

  1. Yes, according to the pca help, "Rows of X correspond to observations and columns to variables."

  2. score just tells you the representation of M in the principal component space. You want the first column of coeff.

  3. numberOfDimensions = 5;
    coeff = pca(A);
    reducedDimension = coeff(:,1:numberOfDimensions);
    reducedData = A * reducedDimension;
    

Upvotes: 5

Related Questions