Simple MATLAB sum question.

1 view (last 30 days)
Myles Baker
Myles Baker on 21 Jan 2011
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

Answers (2)

Walter Roberson
Walter Roberson on 21 Jan 2011
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 on 21 Jan 2011
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

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!