Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Prabha Kumaresan
Prabha Kumaresan 2018 年 1 月 8 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
jaah navi
jaah navi 2018 年 1 月 9 日
Thanks a lot.It worked.
KSSV
KSSV 2018 年 1 月 9 日
Thanks is accepting the answer.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by