Assembly of matrices using for loop

5 ビュー (過去 30 日間)
Gethal
Gethal 2014 年 10 月 30 日
回答済み: Gethal 2014 年 10 月 30 日
Hello everyone! I am trying to make an assembly of matrices. I want to run a for loop that will continuously add to an assembly. For example:
as = [1 1;1 1]
for i=1:2
k = [1 2;3 4]
as = as + k %That "+" should not be addition but assembly
end
If you don't know what assembly is, for the previous case it should look something like this:
as = 1 1 0 0
1 2 2 0
0 3 5 2
0 0 3 4
In other words it is sort of like joining the matrices but by adding the lower right corner of the first with the upper left corner of the second and so on. And fills the rest with zeros. The size is (i+2) by (i+2) in this case. Any idea how to do this?

回答 (1 件)

Gethal
Gethal 2014 年 10 月 30 日
I got it. Use something like this.
h = 2
as = zeros(h+1);
k = [1 2;3 4]
for i = 1:h
as(i:i+1,i:i+1) = as(i:i+1,i:i+1) + k
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by