Making bigger matrix with smaller matrixes elements.

I'm assembling a bigger matrix with elements of other smaller matrixes. in my method I build a connecting vector for example C=[1 5 6 7 8 9] when in each element like for example C(2)=5, 2 is a local dimension in smaller matrix and 5 is a global dimension in main matrix which is assembled.
If I want to use easy code writing I should use this kind of coding:
%for i=1:size(C,2)
for j=1:size(C,2)
B(C(i),C(j))=A(i,j)
but I'm trying to use Matlab special features in matrix calculations to put A with local dimension in B with global dimension. because Matlab features are fast and need less code writing.
Note: A and B are n*n and m*m matrixes.

3 件のコメント

Image Analyst
Image Analyst 2013 年 4 月 21 日
First of all, let's see if this is what you meant (since your code does not run as-is, and I tried to fix it but am not sure I got what you intended):
C=[1 5 6 7 8 9]
A = magic(9); % Some sample data.
for i=1:size(C, 1)
for j=1:size(C, 2)
B(C(i),C(j))=A(i,j);
end
end
B
Results in command window:
C =
1 5 6 7 8 9
B =
47 0 0 0 58 69 80 1 12
Ayob
Ayob 2013 年 4 月 21 日
Yes,you are right.But A and B are both matrixes not vectors.
Image Analyst
Image Analyst 2013 年 4 月 21 日
編集済み: Image Analyst 2013 年 4 月 21 日
Please take note that A is a 2D 9 by 9 matrix. And B ended up as a 1D row vector because your loop over i only went from 1 to size(C,1), in other words from 1 to 1 so it did just one row. That was your code, not mine, though you commented it out. That's why I asked you if my attempts to fix your code were correct, which you said it was.

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

 採用された回答

Matt J
Matt J 2013 年 4 月 21 日

0 投票

B(C,C)=A;

2 件のコメント

Matt J
Matt J 2013 年 4 月 21 日
編集済み: Matt J 2013 年 4 月 21 日
You're confusing people by writing
%for i=1:size(C,2)
with the '%', as if you have commented this line out. Is there a loop over i or not? If yes, why do you have the '%'? And if i runs from 1 to size(C,2)=4 then A needs to have 4 rows in order for A(i,j) to be accessible when i=4.
Ayob
Ayob 2013 年 4 月 21 日
編集済み: Ayob 2013 年 4 月 21 日
Clear and complete answer by Matt J.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

質問済み:

2013 年 4 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by