現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Repeating a code n-times to obtain n-arrays
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Agnieszka Dybalska
2020 年 12 月 2 日
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
2020 年 12 月 2 日
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
2020 年 12 月 3 日
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
2020 年 12 月 3 日
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
2020 年 12 月 3 日
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
2020 年 12 月 3 日
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
2020 年 12 月 3 日
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.
Rik
2020 年 12 月 3 日
If you had use the smart indent you would see that you store the result in your array after the loop, not inside it.
You could also have noticed this if you had put a break point at the start to step through your code line by line.
Agnieszka Dybalska
2020 年 12 月 3 日
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
Rik
2020 年 12 月 3 日
You can activate the smart indent by selecting your code in Matlab and clicking the button in the lint, or use the ctrl-i shortcut.
You can activate a break point by clicking on the dash in front of a line number.
Agnieszka Dybalska
2020 年 12 月 3 日
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.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)