hello i want to loop this function, anyone can help me? thank you

조회 수: 1 (최근 30일)
Gilang
Gilang 2023년 1월 29일
댓글: Luca Ferro 2023년 1월 31일
mdl1 = fitlm(Xtr1,Ytr1,'quadratic');
mdl2 = fitlm(Xtr2,Ytr2,'quadratic');
mdl3 = fitlm(Xtr3,Ytr3,'quadratic');
mdl4 = fitlm(Xtr4,Ytr4,'quadratic');
mdl5 = fitlm(Xtr5,Ytr5,'quadratic');
mdl6 = fitlm(Xtr6,Ytr6,'quadratic');
  댓글 수: 1
dpb
dpb 2023년 1월 29일
Turn the X, Y variables into arrays and then pass to the function by column indices in the loop; save the result as cell array.

댓글을 달려면 로그인하십시오.

채택된 답변

Luca Ferro
Luca Ferro 2023년 1월 30일
Xtr=[Xtr1 Xtr2 Xtr3 Xtr4 Xtr5 Xtr6];
Ytr=[Ytr1 Ytr2 Ytr3 Ytr4 Ytr5 Ytr6];
mdl=[];
for jj=1:lenght(Xtr)
mdl(jj)=fitlm(Xtr(jj),Ytr(jj),'quadratic')
end
as said by dpb in the comments.
  댓글 수: 2
dpb
dpb 2023년 1월 30일
>> mdl(2)=fitlm(x,y,'linear')
Error using classreg.regr.CompactFitObject/subsasgn (line 228)
Assignment using () is not allowed for a FitObject.
>>
As the comments also said, the outputs will have to be saved in a cell array as fitlm returns an object.
"Use the curlies, Luke..." :)
...
mdl={};
for jj=1:size(Xtr,2)
mdl(jj)=fitlm(Xtr(jj),Ytr(jj),'quadratic');
...
Also, length is a very dangerous function being defined as
length(X) <-- max(size(X))
Hence, if OP's various x,y arrays have more than six points, X,Y above will return the number of rows in the combined array, not the number of columns; remember, you're taking the size of the overall new array, there, not counting the number of vectors combined to make the array.
Use size with the specific dimension of interest as above or return both arguments and use the one of interest for the given purpose--
[nr,nc]=size(Xtr); % return array size, rows, columns
for i=1:nc
...
Luca Ferro
Luca Ferro 2023년 1월 31일
oh yeah i'm desolated, didn't check neither knew that ftilm was a matlab function, i thought it was custom made.
I also didn't know about how length has this huge critical aspect, thank you for educating me about it :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by