Hi Antonio,
When dealing with 3D data in MATLAB and you want to apply PCA, you'll need to reshape your data into a 2D matrix because PCA operates on 2D matrices. The idea is to treat each 3D data point as a single observation with multiple features. Here are the steps to achieve this:
% Example 3D data
data = rand(10, 10, 5); % 10x10 grid with 5 layers
% Reshape the 3D matrix into a 2D matrix
% Each row is a flattened version of the 3D grid
[nx, ny, nz] = size(data);
data2D = reshape(data, nx * ny, nz);
% Apply PCA
[coeff, score, latent] = pca(data2D);
% coeff contains the principal component coefficients
% score contains the representation of data in the new PCA space
% latent contains the variance explained by each principal component
% Example: Reconstruct the first principal component back to 3D
pc1 = reshape(score(:, 1), nx, ny);
% Visualize the first principal component as a 2D surface
figure;
surf(pc1);
title('First Principal Component');
xlabel('X');
ylabel('Y');
zlabel('PC1 Intensity');
