Generate random samples at a certain point in Matlab - 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.

Related

How to make sense of principal component analysis (PCA) in 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

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

Selecting random values in a set in mathematica

I have a set which has {0} and other 8 elements, total 9 elements. I want to choose random 3 value in this set and create a 3x1 column matrix. This will repeat all possible choices in the set. How can I do?
As #Picket said in comment,
The way RandomSample works will ensure it will not output the same choice twice in a single call
If your list is small, you can generate all subsets and sample it.
Example
RandomSample[Subsets[{a, b, c, d, e, f}, {3}], 7]
will generate all (20) subsets with 3 (distinct) elements and then pick 7 different uniformly (there are options to weight each member differently, chose the random generator, etc.).
RandomSample[Flatten[Permutations /# Subsets[{a, b, c, d, e, f}, {3}], 1], 13]
will generate all (120) possible ordered selections of 3 distinct elements among a set of 6 elements and give a sample of 13 distinct elements of this list.
If what you want is a random ordering of all possible subsets of size 3, or of all ordered selections without duplicate of size 3 just ask the same way but with the exact number of such sets.
myset = { foo, foo2, foo3, foo5 };
RandomSample[Subsets[myset, {3}], Binomial[Length[myset],3 ]]
RandomSample[Flatten[Permutations /# Subsets[myset, {3}], 1], 3!*Binomial[Length[myset],3 ] ]
(if you ask more than the exact number of possibilities, RandomSample will complain)
Now if your initial set is large so that the set of subsets is impractical for generation time and memory, take advantage of representing set composition by numbers, even if it is not perfect in term of uniform distribution. Say that your initial set has 20 distinct elements. A three digit number in base 20 can represent any selection of 3. If you account for the need to filter out the few with one digit appearing more than once
20^3/(3!*Binomial[20, 3]) // N
1.16959
You are probably safe by generating 25% more numbers than what you need and filtering the ones with repetition:
Cases[IntegerDigits[RandomSample[0 ;; 20^3-1, Ceiling[31*(1 + 1/4)] ], 20, 3], _?(Length[Union[#]] == 3 &), 1, 31]
This generates a random sample of 39 distinct 3-digit numbers in base 20 and select the first 31 with no duplicates in the form of a list of 3-coordinates vectors.

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.