I want to create a block matrix using some loop (and logical conditions). I wrote the following code that works when "p" and "q" are numbers. How can I make it work in the same way, when "p" and "q" are matrices?

2 ビュー (過去 30 日間)
I want to create a large matrix c, where p, q and r are small matrices (of same size). The form of "c" shown here is not fixed, rather it would depend on some logic in my code. For example in this case, the diagonal blocks must be "p" and so on.
c=
[p r q r r
r p r q r
q r p r q
r q r p r
r r q q p]
I wrote the following code, that works only if "p", "q" and "r" are numbers. But they don't work if I put them to be matrix. How can I fix it to make it work for matrices?
clc;
a=1:5;
%p=[2 1;1 2];
%q=[0.3 0.2;0.2 0.3];
%r=[0 0;0 0];
p=5;
q=2;
r=0;
for(i=1:5)
for(j=1:5)
if (abs(a(i)-a(j))==0)
c(i,j)=p;
elseif (abs(a(i)-a(j))==2)
c(i,j)=q;
else
c(i,j)=r;
endif
endfor
endfor
c
Output:
c =
5 0 2 0 0
0 5 0 2 0
2 0 5 0 2
0 2 0 5 0
0 0 2 0 5

回答 (2 件)

Rik
Rik 2017 年 3 月 23 日
編集済み: Rik 2017 年 3 月 23 日
clc;
p=[2 1 ;1 2 ];
q=[0.3 0.2;0.2 0.3];
r=[0 0 ;0 0 ];
c=[p r q r r;...
r p r q r;...
q r p r q;...
r q r p r;...
r r q q p];
c=cell2mat(c);
  2 件のコメント
Md Nur Kutubul Alam
Md Nur Kutubul Alam 2017 年 3 月 23 日
Thank you very much. In the above code we have to define the cell size beforehand while in my case the cell size is unknown. In fact it can change dynamically. For matrix with elements in it, I can easily add new elements/matrix in the existing one like-
if (my_variable==2)
c=[c a q;...
a c q;...
q a c];
Can I do similar things if I go ahead with the cell style? In brief i need full freedom as I have in dealing with matrices.
Rik
Rik 2017 年 3 月 23 日
Sure, you can do what you wrote:
c={1};a={2};q={3};
c=[c a q;...
a c q;...
q a c];
c=cell2mat(c);
As long as you make sure that the elements you put in are all equal sizes, you should be golden.
PS if you found this answer useful, please mark it as accepted answer. It will give us both reputation points and will make it easier for other people with the same question to find an answer.

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


Stephen23
Stephen23 2017 年 3 月 23 日
編集済み: Stephen23 2017 年 3 月 23 日
Here is a simple solution using indexing:
>> C = {[2,1;1,2],[0.3,0.2;0.2,0.3],[0,0;0,0]};
>> V = [1,3,2,3,3]; % define your logic here
>> M = toeplitz(V,V); % create index matrix
>> cell2mat(C(M)) % create output matrix
ans =
2.00000 1.00000 0.00000 0.00000 0.30000 0.20000 0.00000 0.00000 0.00000 0.00000
1.00000 2.00000 0.00000 0.00000 0.20000 0.30000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 2.00000 1.00000 0.00000 0.00000 0.30000 0.20000 0.00000 0.00000
0.00000 0.00000 1.00000 2.00000 0.00000 0.00000 0.20000 0.30000 0.00000 0.00000
0.30000 0.20000 0.00000 0.00000 2.00000 1.00000 0.00000 0.00000 0.30000 0.20000
0.20000 0.30000 0.00000 0.00000 1.00000 2.00000 0.00000 0.00000 0.20000 0.30000
0.00000 0.00000 0.30000 0.20000 0.00000 0.00000 2.00000 1.00000 0.00000 0.00000
0.00000 0.00000 0.20000 0.30000 0.00000 0.00000 1.00000 2.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.30000 0.20000 0.00000 0.00000 2.00000 1.00000
0.00000 0.00000 0.00000 0.00000 0.20000 0.30000 0.00000 0.00000 1.00000 2.00000
MATLAB is a beautiful high-level language, it is not a low-level language like C, and as such it does not rely on ugly loops for solving every task.

カテゴリ

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