Iterating over a vector to create a cell array without for loop

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 comentario

It would be helpful, if you could share your data containing 'datos'.

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentario

Thank you so much. I did want to accelerate the code and it worked perfectly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Dic. de 2020

Comentada:

el 21 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by