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
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 中查找有关 Logical 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by