You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Repeating a code n-times to obtain n-arrays
1 view (last 30 days)
Show older comments
Hi,
I have a code and wish to do small "simulation". The code generates y,x arrays of 10 values, plot them and extract the values a,b from a regresion line.
Also, I obtain R2 for that line. The last result is an array, RESULT = [a,b, R2].
If I wish to reapeat a code n-times, how to built a loop?
How to gather results to compare later?
Another issue is plotting the x,y to get values a,b of the linear regresion line. I don't need the Figure, but I couldn't find a way to skip it.
Do you have a good ideas how to solve this? I am not an experienced user, it is my first matlab code.
Thank you for any help,
Aga.
14 Comments
Agnieszka Dybalska
on 2 Dec 2020
a1 = 3;
b1 = 1:20;
r1 = wblrnd(a1,b1)
% sampling 10 data (needs to be repeated n-times):
s = datasample(r1,10)
% calculation on the data sampled before
s1=sort(s)
x= log(s1)
n = [1,2,3,4,5,6,7,8,9,10]
P = zeros(size(n))
for i = 1:numel(n)
P(i) = (n(i)-0.5)./10;
end
y = ln ln (1/1-P)
y1= 1-P
y2 = zeros (size(y1))
y3 = (y2+1)./y1
y = log (log(y3))
% plotting
scatter (x,y, 25, 'b', '*')
hold on;
lsline;
h = lsline;
% obtain regresion a,b
p2 = polyfit(get(h,'xdata'),get(h,'ydata'),1)
% find r2
[P,S] = polyfit(x,y,1)
R2 = 1 - (S.normr/norm(y – mean(y)))^2
Result = [p2, R2]
Agnieszka Dybalska
on 2 Dec 2020
I wish to reapeat this n-times.
I wish to use something as : for j=1:1000
%code
end
MAin question is if that can be used how to gather results (for example 1000 arrays [a,b,r2]) and analyse them later.
Second question is how to skip plotting, if possible and obtain a,b, r2 .
Thx
David Hill
on 3 Dec 2020
Explain the outputs of the below variables.
wblrnd(a1,b1)%what is wblrnd?
datasample(r1,10)%what is datasample?
Depending on what the above you could loop or even execute without any loops.
n=1000;
P = (1:10-.5)/10;
y=log(log(1./(1-P)));%y appears to never change
for k=1:n
s=datasample()%not sure what this is
x= log(sort(s))
[p,S] = polyfit(x,y,1)
r = 1 - (S.normr/norm(y-mean(y)))^2;
Result(k,:) = [p, r];
end
Agnieszka Dybalska
on 3 Dec 2020
Wblrnd it is weibull modulus. It is as Gaussian model. The function prepare an array with a 20 random values from the model. Sampling is a function which take each time 10 different values from that 20, and later those values are used in the calculations. I wish to repeat the sampling many times and follow the calculations for different sets of 10 values sampled from 20 weibull data.
Agnieszka Dybalska
on 3 Dec 2020
HI,
thank you, after your help my code is shorter and more elegant. I try to do the loop 10 times on the beginning.
After giving the code I am receiving a result 10 times ( REsult: a , b, R in 10 rows , all matrix (3x10) repeated 10 times).
Where is the mistake in my code?
a1 = 3;
b1 = 1:20;
r1 = wblrnd(a1,b1);
% defining y
P= ((1:10) - 0.5)./10;
y=log(log(1./(1-P)));
% sampling data and linear regresion
n=10;
for k=1:n;
s = datasample(r1,10);
x= log(sort(s));
[p,S] = polyfit(x,y,1);
r = 1 - (S.normr/norm(y-mean(y)))^2;
Result(k,:) = [p, r]
end
Thank you :)
Agnieszka Dybalska
on 3 Dec 2020
I took a result out the loop :
for k=1:n;
s = datasample(r1,10);
x= log(sort(s));
[p,S] = polyfit(x,y,1);
r = 1 - (S.normr/norm(y-mean(y)))^2;
end
Result(k,:) = [p, r]
It works as I see.
Thank you.
Agnieszka Dybalska
on 3 Dec 2020
No, after final check it doesn't work. I have one result and 9 zero results in that way. Or 10 results of 3*10 matrix as before... Hope you know where is the error.
Agnieszka Dybalska
on 3 Dec 2020
I am not sure how to put break point or use smart indent but I will check.
At the moment I copied the data, because it appeared.
I know that the code should be corrected. Anyway, I found that the result of polyfit , the a and b are significantely different than calculated by excel so I am trying to solve this problem first. I hope someone will help me to correct the above code so I will have one result :D . Thx
Agnieszka Dybalska
on 3 Dec 2020
Thank you, I will do and check the possibilities. I just solved the data problem ;) , I will go to check rest afterwards, but probably tomorrow sinc it is close to midnight for me.
Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)