Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Could anyone help me how to write the matrix with respect to diagnol in the for loop.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
If i run the following code
N_UE=[2 4 6 8 10];
N_SC=[12 14 16 18 20];
for t= 1:length(N_UE)
for r= 1:length(N_UE)
G=rand(N_UE(t),N_SC(r))
C=[diag(1:N_UE(t)),diag(1:N_SC(r))]
end
end
it results in Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in (line 6) C=[diag(1:N_UE(t)),diag(1:N_SC(r))]
Could anyone help me to solve it.
7 commentaires
KSSV
le 9 Jan 2018
Why don't you give an matrix example...you are expecting, instead of code with errors?
Réponses (2)
Rik
le 9 Jan 2018
I'll add my solution as a separate answer to avoid confusion.
m=[2 4 6 8 10];
n=[12 14 16 18 20];
iwant = cell(length(m),1) ;
for t= 1:length(m)
n_=ceil(n(t)/m(t));%round up to nearest multiple
C=repmat(diag(1:m(t)),1,n_);
iwant{t}=C(:,1:n(t));%crop back to only needed cols
end
For me this just executes as expected. The fourth run yields an 8x18 matrix:
1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
0 0 3 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0 0 0 5 0 0 0 0 0
0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0
0 0 0 0 0 0 7 0 0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 8 0 0
10 commentaires
Rik
le 10 Jan 2018
You should find the button to accept an answer right next to or below the profile picture of the person supplying the answer.
Walter Roberson
le 10 Jan 2018
Rik, it was not jaah who asked the Question so jaah would not be able to Accept the answer.
KSSV
le 9 Jan 2018
k = [1 2] ;
C = diag(k) ;
C1 = repmat(C,1,6) ;
k = [1 2 3 4] ;
C = diag(k) ;
C2 = repmat(C,1,3) ;
C21 = diag([1 2]) ;
C22 = zeros(2) ;
C212 = [C21 ; C22] ;
C2 = [C2 C212] ;
6 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!