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?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
Rik
il 23 Mar 2017
Modificato: Rik
il 23 Mar 2017
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 Commenti
Rik
il 23 Mar 2017
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
il 23 Mar 2017
Modificato: Stephen23
il 23 Mar 2017
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!