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
2018-1-9
Why don't you give an matrix example...you are expecting, instead of code with errors?
回答(2 个)
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
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
2018-1-10
Rik, it was not jaah who asked the Question so jaah would not be able to Accept the answer.
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!