Simple MATLAB sum question.

1 次查看(过去 30 天)
Myles Baker
Myles Baker 2011-1-21
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

回答(2 个)

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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by