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

1 visualizzazione (ultimi 30 giorni)
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 Commento
dpb
dpb il 29 Gen 2023
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.

Accedi per commentare.

Risposta accettata

Luca Ferro
Luca Ferro il 30 Gen 2023
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 Commenti
dpb
dpb il 30 Gen 2023
>> 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 il 31 Gen 2023
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 :)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by