Question about two dimensional PCA analysis

조회 수: 2 (최근 30일)
Jaime  de la Mota
Jaime de la Mota 2018년 6월 19일
답변: Hari 2025년 2월 24일
Hello everybody. I have a file containing wind data in directions S-N and W-E for some places and at some times denoted by latitudes and longitudes. I have written the following code:
if true
clear all
close all
clc
ncdisp('solicitud_31082016_00_12-60_ext.nc')
longitud = ncread('solicitud_31082016_00_12-60_ext.nc', 'longitude');
latitud = ncread('solicitud_31082016_00_12-60_ext.nc', 'latitude');
numero = ncread('solicitud_31082016_00_12-60_ext.nc', 'number');
tiempo = ncread('solicitud_31082016_00_12-60_ext.nc', 'time');
mat_u = ncread('solicitud_31082016_00_12-60_ext.nc', 'u');
mat_v = ncread('solicitud_31082016_00_12-60_ext.nc', 'v');
mat_t = ncread('solicitud_31082016_00_12-60_ext.nc', 't');
latitudes = size (mat_u, 2);
longitudes=size (mat_u, 1);
for contador_latitudes = 1:latitudes
for contador_longitudes =1: longitudes
U =(squeeze(mat_u(contador_longitudes,contador_latitudes,:,:)))';
V =(squeeze(mat_v(contador_longitudes,contador_latitudes,:,:)))';
module=sqrt(U.^2+V.^2);
t=size(V,1);
num_realizations=size(V,2);
Autocovariance=cov(module');
[eigenvectors,eigenvalues]=eig(Autocovariance);
[coeff, score, latent, tsquared, explained, mu]=pca(module,'Centered',false);T=score*coeff';TT=T';
useful_pcs=9;
coeff_global(:,:,contador_latitudes, contador_longitudes)=coeff;
score_global(:,:,contador_latitudes, contador_longitudes)=score;
tsquared_global(:,:,contador_latitudes, contador_longitudes)=tsquared;
explained_global(:,contador_latitudes, contador_longitudes)=explained;
mu_global(:, contador_latitudes, contador_longitudes)=mu;
end
end
end
However, I don't want to know just the module of the wind velocity for all these places at all these times. I want to know what is the relationship between both directions and not just the variation of the module of the wind.
Can someone please tell me how to do that?
Edit: I want to see not the unidimensional eigenvectors, instead I want to visualize a surface with the eigenvectors in both directions.
Thanks

답변 (1개)

Hari
Hari 2025년 2월 24일
Hi,
I understand that you want to perform a two-dimensional Principal Component Analysis (PCA) on wind data to analyze the relationship between wind directions (S-N and W-E) rather than just the magnitude of the wind velocity.
I assume you want to visualize the principal components as a surface, showing the variation in both wind directions across different locations and times.
In order to perform two-dimensional PCA and visualize the results, you can follow the below steps:
Prepare the Data:
Combine the wind components U and V into a two-dimensional dataset for PCA. This allows you to analyze the covariance between both directions.
for lat = 1:latitudes
for lon = 1:longitudes
U = squeeze(mat_u(lon, lat, :, :))';
V = squeeze(mat_v(lon, lat, :, :))';
data = [U(:), V(:)]; % Combine U and V for PCA
Perform PCA on Combined Data:
Use the “pca” function to perform PCA on the combined dataset data. This will provide principal components that capture the covariance between the two wind directions.
[coeff, score, latent, tsquared, explained, mu] = pca(data, 'Centered', false);
Visualize the Principal Components:
You can visualize the principal components as a surface plot. Use “meshgrid” to create a grid and “surf” to plot the eigenvectors.
[X, Y] = meshgrid(1:size(U, 2), 1:size(U, 1));
Z = reshape(score(:, 1), size(U)); % Example: visualize the first principal component
figure;
surf(X, Y, Z);
title(['Principal Component Surface at Lat: ', num2str(lat), ' Lon: ', num2str(lon)]);
xlabel('Time');
ylabel('Realizations');
zlabel('Principal Component');
Store Results for Each Location:
Save the PCA results for each latitude and longitude to analyze them later or compare across different locations.
coeff_global(:, :, lat, lon) = coeff;
score_global(:, :, lat, lon) = score;
explained_global(:, lat, lon) = explained;
mu_global(:, lat, lon) = mu;
end
end
Analyze and Interpret Results:
Examine the explained variance and principal components to understand the relationship between wind directions. Higher explained variance indicates more significant relationships.
Refer to the documentation of “pca” function to know more about its usage: https://www.mathworks.com/help/stats/pca.html
Hope this helps!

카테고리

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