How can i build a for loop for a subset of data having repetitions?

1 回表示 (過去 30 日間)
EM geo
EM geo 2019 年 2 月 22 日
コメント済み: EM geo 2019 年 2 月 22 日
I would like to do a regression analysis with some data in matrix A1 (variables indexed). The variable IDstop1 is repeated many times and i want to do the regression for each subset of Rmean_pred1, Rmean_obs1 and Zero1 where IDstop1 is the same (for example for IDstop1 = 20160008 and then for the next one etc..). I tried this loop but i receive only one value for y, x1, x2, and an error for X. I expect to have the same length of (for example) 20160008 for these variables.
Can someone help me?
IDstop1 = A1(:,1);
Rmean_pred1 = A1(:,15);
Rmean_obs1 = A1(:,14);
Zero1 = A1(:,3)
k = unique(IDstop1);
for i= 1:length(k)
y = Rmean_pred1(i);
x1 = Rmean_obs1(i);
x2 = Zero1(i);
X = [ones(size(x1(i))) x1(i) x2(i)];
b = regress(y,X)
end

採用された回答

Jan
Jan 2019 年 2 月 22 日
編集済み: Jan 2019 年 2 月 22 日
Maybe you want:
k = unique(IDstop1);
result = cell(1, numel(k));
for i = 1:numel(k)
index = (IDstop1 == k(i));
y = Rmean_pred1(index);
x1 = Rmean_obs1(index);
x2 = Zero1(index);
X = [ones(size(x1)), x1, x2];
b = regress(y, X);
result{i} = b;
end
  4 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 22 日
result{1,1}(1,:)
EM geo
EM geo 2019 年 2 月 22 日
perfect! Thanks Madhan Ravi!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by