I would like to know if it is possible to do the same as the code below without a for loop:
for i=1:length(index)
date1{i}=datenum(datos.Date(index(i):end));
Y{i}=(datos.High(index(i):end)+datos.Low(index(i):end))./2;
X{i}=[ones(length(Y{i}),1) date1{i}];
b{i}=regress(Y{i},X{i});
tendency{i}=b{i}(1)+b{i}(2)*X{i}(:,2);
parallel{i}=std(Y{i});
end

1 件のコメント

Abhishek Gupta
Abhishek Gupta 2020 年 12 月 21 日
It would be helpful, if you could share your data containing 'datos'.

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

 採用された回答

Jan
Jan 2020 年 12 月 21 日

1 投票

Most likely this cannot be vectorized. By why do you ask for it? Do you want to accelerate the code? This is possible:
Avoid repeated work. E.g.
date1{i}=datenum(datos.Date(index(i):end));
Convetry many date repeatedly. So move this befor the loop to convert the dates once only:
date1V = datenum(datos.Date);
% and inside the loop:
date1{i} = date1V(index(i):end);
regress() is a very powerful function. All you want to do is a polynomial fit. Then this is probably faster:
function p = TinyPolyFit(x, y, n)
% Construct Vandermonde matrix:
x = x(:);
V = ones(length(x), n + 1);
for j = n:-1:1
V(:, j) = V(:, j + 1) .* x;
end
% Solve least squares problem:
[Q, R] = qr(V, 0);
p = transpose(R \ (transpose(Q) * y(:)));
end

1 件のコメント

Carlos de los Santos
Carlos de los Santos 2020 年 12 月 21 日
Thank you so much. I did want to accelerate the code and it worked perfectly.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by