PCA 3d Matrix

4 次查看(过去 30 天)
Antonio Cabeda
Antonio Cabeda 2016-9-5
回答: Aditya 2025-3-3
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,

回答(1 个)

Aditya
Aditya 2025-3-3
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');

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by