How can I plot the principal component axis? - axis

I used principal component analysis in 8-D data set. now I have principal components, and I want to plot PC1 and PC2 axis on the original axis (x-y) and compare them. but how can I plot two orthogonal 8-D vector (PCs) in 2D dimension in matlab.
I am confused about this. This issue is understandable for me in a two-dimensional dataset, but in more dimensions, I cannot understand how the directions of the perpendicular principal components are determined.

Related

Embedding 2d plot into a 3d plot in matlab?

I have a set of data vectors z that has this 2d plot
How would I go about embed this set of data into a 3d plot like this in matlab? I'm asking for advice and suggestions. The theory I'm trying to employ is "for each data vector~zj, “copies” the data vector intothe first two entries of a 3D data vector~yjand then computes the squared length of~zj as the third entry of~yj. " or kernel trick.
Your 2d data will somehow be in the form, that you have x-coordinates and y-coordinates. Let's say you have a vector x and a vector y for simplification.
As you found out the plot3-function proivdes functionality to plot arbitrary points in 3d without the need of generating a mesh. What you need additionally is a third vector z with data for the 3rd dimension.
So what else can you do? The thing I am thinking about is rotating the plane you are plotting you "2d" data:
Rotational matrices can be seen here:
https://en.wikipedia.org/wiki/Rotation_matrix

Plotting arbitrary 3d finite element mesh with matlab

Hello guys I am trying to export a mesh from MSC Patran and then plot it in Matlab. The mesh can be of arbitrary shape. I have the x, y and z coordinates of all the nodes. So far I have tried many different options and here is why they failed:
Surfc() with meshgrid and griddata:
I generated a grid on x-y plane with meshgrid and then used griddata to obtain the z matrix. But this plot only works when there is only 1 z value corresponding to an x-y pair. In other words, for this to work z must be of type z = f(x,y).
pdegplot() : I found out that matlab can import and plot .stl files. I tried converting my coordinate matrix format and plot it with this function but it doesn't work either. Because apparently in .stl files an edge can not be shared by more than 2 elements. However my FEM files are always (i hope) shell elements. This means 3 or more elements can share the same elements.
Surfc() with 3d meshgrid: I found out that meshgrid() can take 3 inputs (x,y,z) and create a 3d mesh. However this didn't work either. I used a very small mesh with about 1000 nodes and the code was trying to generate 3 matrices with 1000x1000x1000 elements. That means about 3 gb of memory for a 1000 node mesh. Whats more, surfc couldn't plot even that.
Somehow importing other file formats automatically: so far I have been using patran neutral files (.out). I manually read the file and extract x,y,z data from it. Patran can also export to parasolid, iges and step file formats. I looked for direct ways of importing and plotting these in matlab but such functions don't exist as far as I have looked.
Manually generating a grid: Matlab can create 3D objects (like [x,y,z] = sphere()) and Surfc() can plot these despite what I said in (1.) and the x,y,z matrices generated by sphere() are not 3 dimensional like in (3.) so I tried following this and manually generate a 3d grid from my FEM file just for testing. I found that z has repeating columns and in each column (which acts as a layer) there are n values of x and y. When I tried doing the same thing for my mesh manually, surfc() didn't work again. It plotted a really weird shape that I can't even describe.
Finding a 3rd party plotting software: I tried using (light) software like gnuplot and visit but so far I am all wet. I am open to suggestions if you know any (preferably open source) software that can directly plot patran neutral files. But the software has to be capable of contour plotting also. As I am calculating a quantity for each node in Matlab and then plotting its contour on the mesh.
So you can have a tetramesh?
You seem to be working with FEM-stile meshes, thus the standard surface-plotting function wont work. For FEM-meshes of different shape (not tetra) you may need to write your own function...
If you have the grid points and grid cell connectivities in say variables p and c, you can use the external Matlab FEA Toolbox to plot both structured and unstructured grids with for example the plotgrid command
% Cread grid struct.
grid.p = p;
grid.c = c;
grid.a = gridadj(p,c,size(p,1)); % Reconstruct grid cell adjacencies.
grid.b = gridbdr(p,c,grid.a); % Reconstruct boundary information.
grid.s = ones(1,size(c,2)); % Set subdomain numbers to 1 for all grid cells.
% Plot grid.
plotgrid( grid )

Plotting a 2D vector field with a 3D matrix quiver -MATLAB

I am having real trouble with vectors/matrices etc. in matlab.
I have a 3D vector nxmx2 where n is my x-coordinate, m is my y-coordinate and for each (x,y) there are two z values, the first being my velocity in the x-direction, and the second being my velocity in the y-direction.
How do I plot these velocity functions on a x-y graph? I have been trying with things like:
quiver(A)
quiver(A,A,A(:,:,1),A(:,:,2))
But can't seem to get it to work.
The correct way to use quiver for the input you describe is
quiver(A(:,:,1), A(:,:,2));
If you want the axes of the plot to have equal-spaced values different than 1,2,... you should use meshgrid to generate them, as shown in the examples in the quiver documentation.

pca in matlab - 2D curve stretching

I have N 3D observations taken from an optical motion capture system in XYZ form.
The motion that was captured was just a simple circle arc, derived from a rigid body with fixed axis of rotation.
I used the princomp function in matlab to get all marker points on the same plane i.e. the plane on which the motion has been done.
(See a pic representing 3D data on the plane that was found, below)
What i want to do after the previous step is to look the fitted data on the plane that was found and get the curve of the captured motion in 2D.
In the princomp how to, it is said that
The first two coordinates of the principal component scores give the
projection of each point onto the plane, in the coordinate system of
the plane.
(from "Fitting an Orthogonal Regression Using Principal Components Analysis" article on mathworks help site)
So i thought that if i just plot those pc scores -plot(score(:,1),score(:,2))- i'll get the motion curve. Instead what i got is this.
(See a pic representing curve data in 2D derived from pc scores, below)
The 2d curve seems stretched and nonlinear (different y values for same x values) when it shouldn't be. The curve that i am looking for, should be interpolated by just using simple polynomial (polyfit) or circle fit in matlab.
Is this happening because the plane that was found looks like rhombus relative to the original coordinate system and the pc axes are rotated with respect to the basis of plane in such way that produce this stretch?
Then i thought that, this is happening because of the different coordinate systems of optical system and Matlab. Optical system's (ie cameras) co.sys. is XZY oriented and Matlab's default (i think) co.sys is XYZ oriented. I transformed my data to correspond to Matlab's co.sys through a rotation matrix, run again princomp but i got the same stretch in the 2D curve (the new curve just had different orientation now).
Somewhere else i read that
Principal Components Analysis chooses the first PCA axis as that line
that goes through the centroid, but also minimizes the square of the
distance of each point to that line. Thus, in some sense, the line is
as close to all of the data as possible. Equivalently, the line goes
through the maximum variation in the data. The second PCA axis also
must go through the centroid, and also goes through the maximum
variation in the data, but with a certain constraint: It must be
completely uncorrelated (i.e. at right angles, or "orthogonal") to PCA
axis 1.
I know that i am missing something but i have a problem understanding why i get a stretched curve. What i have to do so i can get the curve right?
Thanks in advance.
EDIT: Here is a sample data file (3 columns XYZ coords for 2 markers)
w w w.sendspace.com/file/2hiezc

plot a set of 3D data in different angles in MATLAB

I have a formula that depends on theta and phi (spherical coordinates 0<=theta<=2*pi and 0<=phi<=pi). By inserting each engle, I obtained a quantity. Now I have a set of data for different angles and I need to plot the surface. My data is a 180*360 matrix, so I am not sure if I can use SURF or MESH or PLOT3. The figure should be a surface that include all data and the axes should be in terms of the quantity, not the quantity versus the angles. How can I plot such a surface?
I see no reason why you cannot use mesh or surf to plot such data. Another option I tend to use is that of density plots. You basically display the dependent variable (quantity) as an image and include the independent variables (angles) along the axis, much like you would with the aforementioned 3D plotting functions. This can be done with imagesc.
Typically you would want your axes to be the dependent variables. Could you elaborate more on this point?
If I understand you correctly you have calculated a function f(theta,phi) and now you want to plot the surface containing all the points with the polar coordinated (r,theta,phi) where r=f(theta,phi).
If this is what you want to do, the 2D version of such a plot is included in MATLAB under the name polar. Unfortunately, as you pointed out, polar3 on MatlabCentral is not the generalization you are looking for.
I have been able to plot a sphere with the following code, using constant r=1. You can give it a try with your function:
phi1=0:1/(3*pi):pi; %# this would be your 180 points
theta1=-pi:1/(3*pi):pi; % your 360 points
r=ones(numel(theta1),numel(phi1));
[phi,theta]=meshgrid(phi1,theta1);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
tri=delaunay(x(:),y(:),z(:));
trisurf(tri,x,y,z);
From my tests it seems that delaunay also includes a lot of triangles which go through the volume of my sphere, so it seems this is not optimal. So maybe you can have a look at fill3 and construct the triangles it draws itself: as a first approximation, you could have the points [x(n,m) x(n+1,m) x(n,m+1)] combined into one triangle, and [x(n+1,m) x(n+1,m+1) x(n+1,m+1)] into another...?