How do I run PCA on multiple cell arrays

Hi,
I have a cell array (see attachment) where each cell contains an array with 21 columns (variables). Each cell represents a different participants and is of different length. I would like to run the PCA on each cell in the cell array and collect the outputs (coeff,score,latent,tsquared,explained,mu) for all participants.
The fucntion I want to use is the following with X being my cell array pre_data_1.
[coeff,score,latent,tsquared,explained,mu] = pca(X)
I am specifically interested in getting an "explained" that contains all the explained variables from each participant. In this variable each column should correspond to an explained variable from a cell/ participant.
I wonder if someone could help me with that?

 Réponse acceptée

Image Analyst
Image Analyst le 13 Avr 2022
Did you try something like
numCells = numel(X);
for k = 1 : numCells
% Extract contents of cell into array.
thisX = X{k};
% Do PCA and store results in more cell arrays.
[coeff{k},score{k},latent{k},tsquared{k},explained{k},mu{k}] = pca(thisX);
end
It's untested on your data. It's just off the top of my head.

5 commentaires

lil brain
lil brain le 13 Avr 2022
Hi, thank you. When I run this code it tells me thisX needs to be a numeric array.
thisX is a 2x1 cell array.
Image Analyst
Image Analyst le 13 Avr 2022
So you have a 2-by-1 cell array inside each cell of a parent/containing cell array? What's in those cells? Basically you have to go inside your cells deep enough to get the actual numerical arrays. Attach your cell array in a .mat file with the paperclip icon if you don't know how to do that.
lil brain
lil brain le 13 Avr 2022
Hi, sorry I had already attached it in retrospect. But I decided to just convert all the cells to doubles using cell2mat like so:
pre_data_mat = cellfun(@cell2mat,pre_data1,'UniformOutput',false);
Image Analyst
Image Analyst le 13 Avr 2022
So did my answer work then?
lil brain
lil brain le 13 Avr 2022
yes :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by