How to make sense of principal component analysis (PCA) in MATLAB - matlab

I have a data set of 3 different variables, each variable has 37 data points as follows:
Variable_1 = [0.489274770173646 0.534659090909091 0.496806966618287 0.593160935871933 0.542091836734694 0.514607775477341 0.580715497052410 0.542977656178750 0.624465240641712 0.644904791797447 0.444644611857190 0.464080100125156 0.522286821705426 0.507719139590466 0.612791008830612 0.561735261401557 0.524166666666667 0.526627218934911 0.449009900990099 0.472768878718535 0.488477561567263 0.576187425642902 0.558307692307692 0.609308792372882 0.647109905020352 0.513392857142857 0.454701120797011 0.557692307692308 0.511568509615385 0.440248676030394 0.500000000000000 0.593340146482712 0.518269230769230 0.623676307886835 0.563086974275214 0.609080188679245 0.769444444444444]
Variable_2 = [0.573717948717949 0.489656381486676 0.443821689259645 0.578812453392990 0.678328092243187 0.476432291666667 0.460748792270531 0.593650793650794 0.585645494152717 0.540435139573071 0.536423112870416 0.471528337362342 0.514469014469015 0.459801313718039 0.674409015942826 0.526881720430108 0.437327188940092 0.531890398342160 0.479985035540591 0.449145299145299 0.553381642512077 0.524932614555257 0.652630308880308 0.561587521131090 0.560003234675724 0.537254901960784 0.521990521327014 0.466041489059392 0.571461291800275 0.413770728190339 0.493939393939394 0.458024968229051 0.579528535980149 0.512145748987855 0.567205861018424 0.463562753036437 0.562938596491228]
Variable_3 = [0.630327868852459 0.521367521367521 0.467658730158730 0.485012755102041 0.523217247097844 0.449032738095238 0.574519230769231 0.594594594594595 0.544390243902439 0.581524147097918 0.487662337662338 0.497564726993079 0.417307692307692 0.609668109668110 0.508928571428572 0.511870845204179 0.444067796610169 0.562337662337663 0.494043887147335 0.530476190476191 0.484235294117647 0.502136752136752 0.632418524871355 0.528787878787879 0.619780219780220 0.416958041958042 0.552419354838710 0.586057692307692 0.461351186853317 0.495276653171390 0.524305555555555 0.655671296296296 0.496873496873497 0.462542087542088 0.660491689750693 0.772549019607843 0.558589870903674]
I put all three variables in a matrix, where the columns are the variables and the rows are the 37 data points.
I uses the PCA function in MATLAB and it gives me the following matrix:
PCA = 0.6370 0.3070 0.7071
0.3494 0.7026 -0.6199
0.6871 -0.6420 -0.3403
First Question: What does each row and each column represent in the PCA matrix.
Second Question: How can I use this matrix to plot each variable along its principle component in 3 dimensions.
Thank you, I very appreciate any help

Related

Generate random samples at a certain point in Matlab

I have the following two data set:
A= (1,1) 1.9643
(2,1) 2.2753
(3,1) 2.9781
(4,1) 2.9320
(5,1) 3.5772
(6,1) 4.0150
(7,1) 3.3814
(8,1) 3.9113
(9,1) 2.7822
(10,1) 2.5518
(11,1) 3.3698
(12,1) 3.1048
(13,1) 1.9182
(14,1) 2.2638
(15,1) 2.6555
(16,1) 3.1855
(17,1) 3.6579
(18,1) 3.9113
(19,1) 3.6002
(20,1) 3.0357
(21,1) 1.5841
(22,1) 2.0103
(23,1) 1.9527
(24,1) 2.2753
(25,1) 2.3099
(26,1) 2.8283
(27,1) 3.0473
(28,1) 2.4827
(29,1) 2.5057
(30,1) 1.8721
(31,1) 2.0103
(32,1) 1.2269
(33,1) 1.8951
(34,1) 1.5610
(35,1) 1.5495
(36,1) 1.6878
(37,1) 1.4919
(38,1) 0.9620
(39,1) 1.1693
(40,1) 0.8122
(41,1) 0.9735
(42,1) 1.2500
(43,1) 1.3191
(44,1) 2.2292
(45,1) 2.4482
(46,1) 2.7938
(47,1) 2.0910
(48,1) 2.5403
(49,1) 0.9044
(50,1) 0.7662
(1,2) 4.5957
(2,2) 3.8589
(3,2) 4.5651
(4,2) 3.5519
(5,2) 2.8560
(6,2) 3.1937
(7,2) 3.4291
(8,2) 4.1761
(9,2) 4.0431
(10,2) 4.6162
(11,2) 3.9101
(12,2) 3.0709
(13,2) 4.0534
(14,2) 4.3706
(15,2) 3.5008
(16,2) 4.2888
(17,2) 3.8692
(18,2) 3.4291
(19,2) 3.1221
(20,2) 3.3165
(21,2) 3.3575
(22,2) 3.2039
(23,2) 2.7843
(24,2) 2.7127
(25,2) 2.9584
(26,2) 2.6309
(27,2) 2.2931
(28,2) 2.0373
(29,2) 2.3853
(30,2) 2.0577
(31,2) 2.3546
(32,2) 2.3239
(33,2) 2.9174
(34,2) 3.0709
(35,2) 2.6923
(36,2) 2.4057
(37,2) 2.0271
(38,2) 2.6820
(39,2) 2.9276
(40,2) 2.9992
(41,2) 3.3881
(42,2) 3.1937
(43,2) 3.5109
(44,2) 2.2010
(45,2) 2.6411
(46,2) 1.9656
(47,2) 1.6177
(48,2) 2.8867
(49,2) 3.0198
(50,2) 2.5899
Here, A is two dimensional sparse matrix where (i,i).
And B is (50,1) matrix, where the first 20 values are +1 and last 30 values are -1.
So the data point is look like this:
Now, I want to choose the (40,50) position of A and then want to generate 1000 random points within +/- 2 regions of the points.
So, the graph should be something like this:
Please let me know, how can I do it in matlab.
Since you don't mention a programming language, here is the general solution:
Assuming you want the data to be centered around (x,y), spread s, and label l.
Then you would for each object generatw two random Gaussians, scale them by the desired spread s, add the center vector (x,y), and append this vector to your data with the desired label.
Here, your center would be something like (45,45) and spread maybe 5 or just 1.
This generates and plots 1000 uniformly distributed random points within a +/- 2 region of position (40,50):
plot (40+4*rand(1000,1)-2, 50+4*rand(1000,1)-2,'o')
I know I ignored the "choosing a position from sparse matrix A"-part of your question. I hope that part is not crucial, and this will help you along anyway.
When you want other random distributions, check out randn (for gaussian) or other rand* functions.

Matlab compare 2 single column matrixes and give position

I'm making a GUI in matlab to calculate ideal shifting points for a racecar.
For this I need to compare 2 single column matrixes.
Fwheel1 =
1.0e+003 *
4.5433
4.6372
4.6770
4.6892
4.7235
4.8064
4.9451
5.0838
5.2300
5.3401
5.4864
5.5454
5.5046
5.4758
5.5028
5.5782
5.6183
5.6663
5.7380
5.8174
5.8940
5.9553
6.0364
6.1075
6.0904
5.9285
5.7654
5.5762
5.3498
5.1766
5.0548
4.8236
4.6538
Fwheel2 =
1.0e+003 *
3.5174
3.5901
3.6209
3.6304
3.6569
3.7211
3.8285
3.9358
4.0490
4.1343
4.2475
4.2932
4.2617
4.2393
4.2602
4.3186
4.3496
4.3868
4.4423
4.5038
4.5631
4.6105
4.6734
4.7284
4.7151
4.5898
4.4635
4.3170
4.1418
4.0077
3.9134
3.7344
3.6029
These are the 2 matrixes. Now what I want is to compare Fwheel1 with Fwheel2. I want to know at which position in the matrix Fwheel2 > Fwheel1.
So output needs to be for example 23.
I hope somebody can help me.
Kind regards
You can do this easily with find.
idx= find( Fwheel2 > Fwheel1);
If you just want the first one, or the first n, you can just
idx= find( Fwheel2 > Fwheel1,n);
for another method,
c=0;
for i=1:33
if Fwheel2(i)>Fwheel1(i)
c=c+1;
b[c]=i
end
end
in the b vector you have your answer

Save outputs of nested for loops in MATLAB

I have the following codes which I wish to have an output matrix Rpp of (10201,3). I run this code (which takes a bit long) then I check the matrix size of Rpp and I see (1,3), I tried so many things I couldn't find any proper way. The logic of the codes is to take the 6 values (contain 4 constant values and 2 variable values (chosen from 101 values)) and make the calculation for 3 different i1 and store every output vector of 3 in a matrix with (101*101 (pairs of those 2 variable values)) rows and 3 (for each i1) columns.
I appreciate your help
Vp1=linspace(3000,3500,101);
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=linspace(1250,1750,101);
Vs2=2000;
i1=[10 25 40];
Rpp = zeros(length(Vp1)*length(Vs1),length (i1));
for n=1:length(Vp1)*length(Vs1)
for m=1:length (i1)
for l=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(l);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(l)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(l)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp=((b.*(cos(i1)/Vp1(l))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(l))).*(cos(j2)/Vs2)).*H.*p.^2)./D
end
end
end
end
Try this. You 2 outer loops didn't do anything. You never used m or n so I killed those 2 loops. Also you just kept overwriting Rpp on every loop so your initialization of Rpp didn't do anything. I added an index var to assign the results to the equation to what I think is the correct part of Rpp.
Vp1=linspace(3000,3500,101);
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=linspace(1250,1750,101);
Vs2=2000;
i1=[10 25 40];
Rpp = zeros(length(Vp1)*length(Vs1),length (i1));
index = 1;
for l=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(l);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(l)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(l)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp(index,:)=((b.*(cos(i1)/Vp1(l))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(l))).*(cos(j2)/Vs2)).*H.*p.^2)./D;
index = index+1;
end
end
Results:
>> size(Rpp)
ans =
10201 3
The way you use the for loop is wrong. You're running the calculation for length(Vp1)*length(Vs1) * length (i1) * length(Vp1) * length(Vs1) times. Here's the correct way. I changed l into lll just so I won't confuse it with the number 1. In each iteration of the first for loop, you're running length(Vs1) times, and you need to assign the result (a 1X3 array) to the Rpp by using a row number specified by k+(lll-1)*length(Vp1).
for lll=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(lll);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(lll)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(lll)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp(k+(lll-1)*length(Vp1),:)=((b.*(cos(i1)/Vp1(lll))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(lll))).*(cos(j2)/Vs2)).*H.*p.^2)./D;
end
end

Explicit formula between matlab neural network output and input

Hello I have a question regarding my neural network. I use a neural network with two hidden layers with activation functions of tansig and purelin. I have 4 inputs and one output.
I trained my network with 25 neurons in layer 1 and 1 neuron in layer 2. Here is the schematic vie of my system
There is a good agreement between my ANN outputs and my targets
But after training the network when I want to derive out the equations of the system strange thing happens
I find the weights and bias of the system as follow
b1=First layer bias= net.b{1}
b2=Second layer bias=net.b{2}
Firstlayer weight net.IW{1,1}
Secondlayer weight net.LW{2,1}
And when I use this formula:
Y=b2+secondlayer weight*tanh((firstlayerweight*X+b1))
(Where X is my input) there is a large difference between my measured data and results of this formula (like the outputs I get are 1000 times larger than they should be).
Can anyone help me with this? Is this formula correct?
Edit:
Here are my weight and bias matrices:
%first layer weights(4*25):
111.993 48.59439 1.604747 -0.76531
10.8245 -107.793 173.7258 -123.2
-149.48 -105.421 102.6071 -79.2133
226.2926 -621.099 2440.947 776.9815
-66.5572 -116.593 -121.067 -55.3703
-9.1293 1.251525 2.716534 -0.3201
0.786728 -1.81314 -5.21815 1.619898
71.98442 -3.67078 -17.8482 5.911515
6.986454 -36.9755 -21.4408 1.50746
0.654341 -5.25562 10.34482 4.589759
0.304266 1.645312 5.004313 -1.51475
0.721048 -0.02945 -0.09663 -0.0004
60.96135 1.182228 4.733361 -0.40264
-1.58998 1.920395 5.533581 -1.71799
0.410825 0.056715 4.564767 -0.1073
2.298645 9.923646 82.42445 -8.89119
-2.46618 -1.59946 -3.41954 -2.68133
0.089749 -1.69783 -5.02845 1.541547
3.516938 0.358534 -10.9719 -0.33401
2.392706 -1.99236 -5.89471 1.815673
1.605158 4.174882 4.227769 -3.14685
-25.2093 -1.68014 -5.249 1.163255
52.30716 -67.498 87.13013 29.61436
9.195869 2.328432 -7.59047 -1.42355
3.120973 1.266733 8.182079 0.365206
%first layer biases(25):
47.07941005
-49.66890557
80.2745463
1251.640193
-228.1521936
-2.905218293
-2.802770641
52.59183653
-50.96014049
7.931255305
3.652513556
-0.125595632
40.47792689
2.343663068
1.712611331
67.61242524
-6.124504787
-3.288283849
-4.752833412
-1.921129618
6.818490015
-6.481531096
5.056644951
1.984717285
7.050001634
%second layer weights(25):
-97.96825145 122.9670213 -124.5566672 -0.046986176 -0.021205068 -5.990650681 1850.804141 2.964275181 -0.333027823 -0.070420859 -583.478589 -68.16211954 12.59658596 1257.471165 -138.4739514 0.07786882 0.238340539 -1546.523224 -2.751936791 363.5612929 -0.152249267 -20.71141251 0.094593198 -0.473042306 5.533999251
%second layer bias(1):
21.92849
For example when I put X=[8;400;5;9.5] I expect to get y = 20.8
But by using the formula
y=secondbias +secondlayer weight* tanh(firstlayer weight*X+first bias)
I get -111 for y which is strange

How to use interp1 in matlab

I have the data
x=[14.0716101600000 17.7292101600000 21.3868101600000 21.3868101600000 25.0444101600000 28.7020101600000 32.3596101600000 36.0172101600000 39.6748101600000 43.3324101600000 46.9900101600000 50.6476101600000 54.3052101600000 57.9628101600000 61.6204101600000 65.2780101600000 68.9356101600000 72.5932101600000 76.2508101600000 79.9084101600000 83.5660101600000 87.2236101600000]
y=[0 0 0 0.152400000000000 0.152400000000000 0.533400000000000 0.685800000000000 0.838200000000000 0.990600000000000 1.14300000000000 1.14300000000000 1.14300000000000 1.29540000000000 1.44780000000000 1.60020000000000 1.75260000000000 1.90500000000000 2.05740000000000 2.20980000000000 2.36220000000000 2.51460000000000 2.66700000000000]
new_x=[21.38,25.03,28.70,32.33,35.99,39.55,45.13,48.80,54.38,58.05];
However, when I use interp1 like
new_y = interp1(x,y,new_x);
it doesn't work.Please tell me why.Thank you so much!
The x vector must be strictly monotonic. In your data, third and fourth elements of x are equal. If you remove, say, fourth element from x and y by
x(4)=[];
y(4)=[];
it works without problem.