Azzera filtri
Azzera filtri

Cell and Mat problem

1 visualizzazione (ultimi 30 giorni)
raj singh
raj singh il 26 Giu 2016
Commentato: raj singh il 26 Giu 2016
A=[1 2;5 2]
B=[2 1;4 5]
I want this as
Ans:
C=[1 2 0 0
5 2 0 0
0 0 2 1
0 0 4 5]
Actualy I want to store matrixes in a single matrix (as given in Ans) because my prog generate 10 matrix.

Risposta accettata

Stephen23
Stephen23 il 26 Giu 2016
Modificato: Stephen23 il 26 Giu 2016
You can use blkdiag:
>> A = [1,2;5,2];
>> B = [2,1;4,5];
>> blkdiag(A,B)
ans =
1 2 0 0
5 2 0 0
0 0 2 1
0 0 4 5
If you are generating those matrices in a loop, then you can simply put them into a cell array first:
>> N = 5;
>> C = cell(1,N);
>> for k = 1:N; C{k} = randi(9,2); end % <- your loop
>> blkdiag(C{:})
ans =
1 2 0 0 0 0 0 0 0 0
3 2 0 0 0 0 0 0 0 0
0 0 3 1 0 0 0 0 0 0
0 0 4 9 0 0 0 0 0 0
0 0 0 0 9 5 0 0 0 0
0 0 0 0 5 4 0 0 0 0
0 0 0 0 0 0 9 2 0 0
0 0 0 0 0 0 4 8 0 0
0 0 0 0 0 0 0 0 4 4
0 0 0 0 0 0 0 0 3 1

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by