How can I run PCA on a 3-dimensional cell arrays?
2 views (last 30 days)
Show older comments
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.
This is what I have until now:
numCells = numel(pre_data1);
for k = 1 : numCells
for p = 1 : n
thisX = pre_data1{k}{p};
[coeff{k}{p},score{k}{p},latent{k}{p},tsquared{k}{p},explained{k}{p},mu{k}{p}] = pca(thisX);
end
end
But when I run this I only get strange results like so:
explained =
1×2 cell array
{1×1 cell} {1×1 cell}
I wonder if someone could help me with that?
0 Comments
Answers (1)
Sai Pavan
on 12 Oct 2023
Hi,
I understand that you are trying to run the PCA algorithm on the 3D cell array of several participants and get the “explained” parameter value of all participants.
Please refer to the below modified code snippet to collect the outputs of PCA for each participant in the cell array:
numCells = numel(pre_data1);
explained = cell(numCells, 1); % Initialize explained variable
for k = 1:numCells
numParticipants = numel(pre_data1{k});
explained{k} = cell(numParticipants, 1); % Initialize explained variable for each cell
for p = 1:numParticipants
thisX = pre_data1{k}{p};
[~, ~, ~, ~, explained{k}{p}, ~] = pca(thisX);
end
% Concatenate the explained variables for each participant into a single matrix
explained{k} = cat(2, explained{k}{:});
end
% Concatenate the explained variables from all cells into a single matrix
explained = cat(2, explained{:});
Hope it helps.
Regards,
Sai Pavan
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!