Simple MATLAB sum question.

I am running a very old MATLAB 6.1. Anyways, I am have:
Ax=b
A=[a11 a12 a13;
a21 a22 a23;
a31 a32 a33]
x=[x1;
x2;
x3]
I want to write a MATLAB program that simply calculates:
b1=x1*a11+x1*a12+x1*a13
...
And the current code I have:
b(1:n,1)=zeros(n,1);
for j=1:n
b(j,1)=x(j,1)*A(1,j)
end
Does not work. I have tried adding a nested for loop. No luck. I have tried the sum command. No luck.
Basically I want to do:
sum(A(1,:)+A(2,:)+...+A(j,:))
In a controlled loop sequence. I have tried:
for j=1:n
sum(A(j,:))
end

Réponses (2)

Walter Roberson
Walter Roberson le 21 Jan 2011

1 vote

Have you tried sum(A(1:j,:),2) ? I am referring to your "Basically I want to do" portion of your question, which otherwise appears to be incomplete in that it does not have the multiplication by b .
Does your version of Matlab not support matrix multiplication,
b = A*x;
Matt Fig
Matt Fig le 21 Jan 2011

0 votes

Is this what you are after?
b = zeros(3,1);
for ii = 1:3
for jj = 1:3
b(ii)= b(ii) + A(ii,jj)*x(jj);
end
end

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by