이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)