PCA 3d Matrix
4 views (last 30 days)
Show older comments
Hello,
I'm trying to use the pca function from matlab to generate loads that I can use to forecast a 3d surface. Having said that, all the examples that I could find only talk about a 2d matrix (row representing the difference values that the column variables assume) Does somebody has an good approach to deal with 3d data ? matrix(x,y,z) instead of a matrix(x,y) ? One way that I thought was to "unwrap" my data, so I transform all the row collumn combinations in different variables (so a matrix 3d 2x2 matrix become a 1d with 4 variables (A,C - A,D - B,C - B,D ) But I don't know if that is the correct approach
Any help will be more than welcomed :)
Cheers,
0 Comments
Answers (1)
Aditya
on 3 Mar 2025
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');
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!