Creating a loop for matrix multiplication
3 次查看(过去 30 天)
显示 更早的评论
Hi. I am trying to create a loop to multiply two matricies 40 times such that A*B0=B1 and A*B1=B2 and so on. So far I have got this
%
a=1-0.0277;
b=(1/15)*(1-0.008);
c=0.0215*a;
d=(14/15)*(1-0.008);
A=[[a,b];[c,d]];
B0=[6820;2140];
B=[B0];
for j=1:40
B1=A*B0;
B0=B1;
B=[B;B1];
disp(round(B(j)))
end
plot(B)
It does work but the result comes up as a list of all the values together and I want it to be a in a 1x2 matrix format.
Can anyone help? Thank you
5 个评论
John D'Errico
2014-5-12
NO NO NO NO. Do not create 81 different named variables. Learn to use arrays of variables. Your code will be the better for it. Your mental health a bit better too, because your code will not drive you crazy.
回答(1 个)
Wojtek
2014-5-13
1 个评论
Jos (10584)
2014-5-13
for i=1:41,
C = [C B(2*i-1:2*i)]
end
is exactly the same as
C = B(1:82)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!