hello i want to loop this function, anyone can help me? thank you
5 ビュー (過去 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 で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!