Matrix multiplication in a for-loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ammar G.
le 5 Fév 2020
Réponse apportée : JESUS DAVID ARIZA ROYETH
le 5 Fév 2020
I'm currently trying to transform this expression:
k = 1:m;
t1 = sum((theta(1) + theta(2) .* X(k,2)) - y(k));
t2 = sum(((theta(1) + theta(2) .* X(k,2)) - y(k)) .* X(k,2));
theta(1) = theta(1) - (alpha/m) * (t1);
theta(2) = theta(2) - (alpha/m) * (t2);
into a for loop expression, so that it will work for longer theta vectors. So far I came up with this:
for j=1,length(theta)
t = sum(((X * theta)- y) .* X(:,j));
theta(j) = theta(j) - (alpha/m) * t;
end
But the values for theta(2) are not updated. What am I doing wrong?
0 commentaires
Réponse acceptée
JESUS DAVID ARIZA ROYETH
le 5 Fév 2020
you have 1,length(theta), change it by 1:length(theta) :
for j=1:length(theta)
t = sum(((X * theta)- y) .* X(:,j));
theta(j) = theta(j) - (alpha/m) * t;
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!