Creating a loop for matrix multiplication

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 件のコメント

dpb
dpb 2014 年 5 月 11 日
編集済み: dpb 2014 年 5 月 11 日
How can you have a 1x2 matrix--you've got two of them which you've concatenated together vertically with the ';'. You can get a 2x2 if you simply horizontally concatenate as
B=[B B1];
but there are four elements. This is for the first pass -- after that you've got a difficulty in dimensions going forward. It's not at all clear what your end objective is, so not sure how to suggest a correction.
Star Strider
Star Strider 2014 年 5 月 11 日
編集済み: Star Strider 2014 年 5 月 11 日
B actually is in an array. Changing that line to:
disp(round(B(:,j)))
will display the new columns of B as they are created.
Else, I agree.
dpb
dpb 2014 年 5 月 11 日
@Star== Whiff... :) Edited the bonehead comment out.
Wojtek
Wojtek 2014 年 5 月 12 日
編集済み: Wojtek 2014 年 5 月 12 日
Basically, this loop creates an array B which has 82 entries in one column. What I would like to have is 81 separate arrays created using elements of B.
So for example C1=B(1:2,1), C2=B(3:4,1) and so on.
I have tried to write something like this:
for k=1:41
for a=1:2:81
for b=2:2:82
C(k)=B(a:b,1)
disp(C)
end
end
end
But that doesn't work.
John D'Errico
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
Wojtek 2014 年 5 月 13 日

0 投票

Problem solved. I have used this
x=length(B);
y=x/2;
C=[];
for i=1:41
C=[C B(2*i-1:2*i)];
end
Thank you all for your comments.

1 件のコメント

Jos (10584)
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)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2014 年 5 月 11 日

コメント済み:

2014 年 5 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by