Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Could anyone help me how to write the matrix with respect to diagnol in the for loop.

1 Ansicht (letzte 30 Tage)
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 Kommentare
KSSV
KSSV am 9 Jan. 2018
Why don't you give an matrix example...you are expecting, instead of code with errors?
Prabha Kumaresan
Prabha Kumaresan am 9 Jan. 2018
C1=[1 0 1 0 1 0 1 0 1 0 1 0;
0 2 0 2 0 2 0 2 0 2 0 2]
C2=[1 0 0 0 1 0 0 0 1 0 0 0 1 0;
0 2 0 0 0 2 0 0 0 2 0 0 0 2;
0 0 3 0 0 0 3 0 0 0 3 0 0 0;
0 0 0 4 0 0 0 4 0 0 0 4 0 0]

Antworten (2)

Rik
Rik am 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 Kommentare
Rik
Rik am 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
Walter Roberson am 10 Jan. 2018
Rik, it was not jaah who asked the Question so jaah would not be able to Accept the answer.

KSSV
KSSV am 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 Kommentare

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by