Hey, Need some help on using PCA on matlab.

So I'm in my masters thesis and I want to use PCA to understand and analyse a 3D movement (elevation angles of FOOT, SHANK and THIGH segments). I already have all the data (3variables'foot/shank/thigh' of 180 observations) but I just don't know how to get the 3 principal vectors. I tried some scripts but I dont really understand them and it doesnt seem to work. I have no matlab experience and they are no good tutorial that shows exemple of people using there data and getting the results.If anyone could help me out it would be nice :) Thank you.

1 comentario

Jillian Beacon
Jillian Beacon el 6 de Oct. de 2019
Did you ever find out how to do this? I am doing something similar in my Ph.D. and I am looking for help with PCA. Would you be willing to chat with me about it?

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 21 de Jul. de 2018

1 voto

Attached is an example of how to use PCA on a 3-D sample.

6 comentarios

Sim Will
Sim Will el 22 de Jul. de 2018
Thank you for the answer and your time but I'm trying to calculate PC1 PC2 and PC3 from the data that gives this loop. The data are all the black dots making a kind of 3d loop. The data was acquired from vicon motion capture. I have 180 frames of data. I'm trying to compare movements of a specialist hockey player and a non specialist during one gait phase before shooting and also compare there movements between repetitions. I'm a bit lost in statistics ^^ thx for any advice or help :)
Image Analyst
Image Analyst el 22 de Jul. de 2018
Yep, my code should do it. Did you try to adapt it? If you did and can't figure it out, attach your data in a csv or .mat or text file and I'll do it for you.
Sim Will
Sim Will el 22 de Jul. de 2018
hey again :) Yes I tried to adapt it but It just seems way to complicated^^ That would be very nice from you if you could do it and show me the code you used with my data so I can understand because I have plenty of trials to analyse. I put my data in a matlab file name: Subject7trial4A where there are 3 columns, 1st column is the Foot angles, 2nd column shank angles and 3rd column thigh angles. If you need any other information just let me know and I can't thank you enough, I don't think I could have managed on my own.
It's really very easy. Essentially 2 lines of code:
% Now get the principal components.
coeff = pca(Subject7Trial4A);
% Take the coefficients and transform the N-by-3 list into a PCA list.
principalComponents = Subject7Trial4A * coeff;
Here is a full demo:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% Check that user has the Statistics Toolbox installed.
% This is needed for the PCA function.
hasStatsToolbox = license('test', 'statistics_toolbox');
if ~hasStatsToolbox
ver % List what toolboxes the user has licenses available for.
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Statistics and Machine Learning Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in data.
fullFileName = fullfile(pwd, 'Subject7Trial4A.mat')
% Open the file.
s = load(fullFileName)
Subject7Trial4A = s.Subject7Trial4A
% Plot them in 3-D
subplot(1, 2, 1);
plot3(Subject7Trial4A(:, 1), Subject7Trial4A(:, 2), Subject7Trial4A(:, 3), 'b.-', 'LineWidth', 2, 'MarkerSize', 20);
grid on;
title('Subject7Trial4A in original Coordinates', 'FontSize', fontSize);
xlabel('Column 1', 'FontSize', fontSize);
ylabel('Column 2', 'FontSize', fontSize);
zlabel('Column 3', 'FontSize', fontSize);
% Now get the principal components.
coeff = pca(Subject7Trial4A);
% Take the coefficients and transform the N-by-3 list into a PCA list.
principalComponents = Subject7Trial4A * coeff;
% Plot them in 3-D
subplot(1, 2, 2);
plot3(principalComponents(:, 1), principalComponents(:, 2), principalComponents(:, 3), 'r.-', 'LineWidth', 2, 'MarkerSize', 20);
grid on;
title('Subject7Trial4A in PC Coordinates', 'FontSize', fontSize);
xlabel('PC #1', 'FontSize', fontSize);
ylabel('PC #2', 'FontSize', fontSize);
zlabel('PC #3', 'FontSize', fontSize);
Sim Will
Sim Will el 23 de Jul. de 2018
Thank you for that :) But I'm still wondering how to get the first three eigenvectors and there eigenvalues so I can compare to other data. Ok I'm just going to tell you in general what I need to do for my thesis so you can tell me if im on the right path or completely doing the wrong things^^. I'm trying to see if there are any differences intra and inter 2xsubjects (specialized and non specialized). I did 10xtrials for both using vidcon motion capture and then I calculated elevation angles of thigh shank and foot rigid segments for each trials. First thing that I wanted to do is compare the 10 trials of the same subject to see if there was any differences or similarity (to see if good Reproducibility ---> I was thinking using PCA as it can show the vectors of the best fitting plane of the data or using a similarity index but then I would have to compare thigh-shank ; shank-foot and thigh-foot) Then my second objective is to compare specialist and non specialist movement and see the differences ( could be useful to know which part of the movement is significantly different ''to work on this motion especially''). I'm not good at all in statistics this is why I don't really know what to use and how to use. Thx for the help
Image Analyst
Image Analyst el 23 de Jul. de 2018
Sounds reasonable. Since you're at a university you can get free statistical help. I suggest you enlist a statistics/math professor at your university to be an adviser, either on your committee or maybe just for a few consulting sessions. He or she will be better at stats than me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 21 de Jul. de 2018

Comentada:

el 6 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by