Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
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
KSSV
KSSV 2018년 1월 9일
Why don't you give an matrix example...you are expecting, instead of code with errors?
Prabha Kumaresan
Prabha Kumaresan 2018년 1월 9일
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]

답변 (2개)

Rik
Rik 2018년 1월 9일
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
Rik
Rik 2018년 1월 10일
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 2018년 1월 10일
Rik, it was not jaah who asked the Question so jaah would not be able to Accept the answer.

KSSV
KSSV 2018년 1월 9일
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

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by