Vectorization of for loop

1 回表示 (過去 30 日間)
Safwana Razak
Safwana Razak 2021 年 7 月 15 日
回答済み: Jayant Gangwar 2021 年 7 月 15 日
B = randi(10x5);
x = randi(10x40);
y = randi(10x1);
% Modelfun = equation to fit
% I can do a for loop like this:
for i=1:10
[beta(i,:)]=nlinfit(x(i,:),y(i,:),modelfun,B)
end
Can I do vectorization? for example 10 fittings all at once, without using loops? or maybe using @cellfun or @arrayfun?
  1 件のコメント
KSSV
KSSV 2021 年 7 月 15 日
編集済み: KSSV 2021 年 7 月 15 日
cellfun, arrayfun also uses loop inside...

サインインしてコメントする。

回答 (1 件)

Jayant Gangwar
Jayant Gangwar 2021 年 7 月 15 日
It seems to me that you want to avoid the use of loops for finding all the rows of beta, You can do it by directly passing the complete x matrix and y vector to the nlinfit function, It will automatically save the answer in different rows of beta. An example of the same is given below-
S = load('reaction');
X = S.reactants; % 13x3 matrix
y = S.rate; % 13x1 vector
beta0 = S.beta;
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(X,y,@hougen,beta0,'ErrorModel','combined');
beta
This is an example given in the documentation for nlinfit, for more information please take a look at the documentation for nlinfit - Nonlinear regression - MATLAB nlinfit (mathworks.com)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by