hello i want to loop this function, anyone can help me? thank you
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!