Calculations/Data-Analyzer/+Analyzer/conductPCA.m

27 lines
812 B
Matlab

function results = conductPCA(od_imgs)
%% computePCAfromImages: Performs PCA on optical density images and returns results in a struct
%
% Inputs:
% od_imgs - cell array of OD images
%
% Outputs:
% pcaResults - struct containing PCA outputs:
% .coeff - PCA coefficients (principal components)
% .score - PCA scores for each image
% .explained - variance explained by each PC
% .Nx, .Ny - dimensions of individual images
allImgs3D = cat(3, od_imgs{:});
[Nx, Ny] = size(allImgs3D(:,:,1));
Xall = reshape(allImgs3D, [], numel(od_imgs))';
[coeff, score, ~, ~, explained] = pca(Xall);
results = struct( ...
'coeff', coeff, ...
'score', score, ...
'explained', explained, ...
'Nx', Nx, ...
'Ny', Ny ...
);
end