フィルターのクリア

Info

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

Matrix manipulation using reshape

1 回表示 (過去 30 日間)
Prabha Kumaresan
Prabha Kumaresan 2017 年 12 月 18 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
If A=
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 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 0 0 0 5 0 0 0 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 0 0 0 8 0 0
0 0 0 0 0 0 0 0 9 0
0 0 0 0 0 0 0 0 0 10
I got B =
1 2 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0
0 0 0 0 5 6 0 0 0 0
0 0 0 0 5 6 0 0 0 0
0 0 0 0 0 0 7 8 0 0
0 0 0 0 0 0 7 8 0 0
0 0 0 0 0 0 0 0 9 10
0 0 0 0 0 0 0 0 9 10
using
B = reshape(repmat(max(reshape(A,2,[])),2,1),size(A)).
But if i want to get C =
1 0 3 0 0 0 0 0 0 0
0 2 0 4 0 0 0 0 0 0
1 0 3 0 0 0 0 0 0 0
0 2 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 8 0 0
0 0 0 0 0 6 0 0 9 0
0 0 0 0 0 0 7 0 0 10
0 0 0 0 5 0 0 8 0 0
0 0 0 0 0 6 0 0 9 0
0 0 0 0 0 0 7 0 0 10
from A how the reshape can be written?
  2 件のコメント
Roger Stafford
Roger Stafford 2017 年 12 月 18 日
How would you generalize what is desired for an arbitrary size diagonal matrix? It is fairly easy to produce the desired result for your particular size.
Prabha Kumaresan
Prabha Kumaresan 2017 年 12 月 18 日
I need to have random grouping of rows which reults in sharing their values.

回答 (1 件)

Roger Stafford
Roger Stafford 2017 年 12 月 18 日
If the "grouping" is to be randomly determined, do this:
n = size(A,1);
p = mod((0:n-1)+randi(n-1,1,n),n)+1; % p(k) never equals k
B = A;
for k = 1:n
B(p(k),k) = B(k,k);
end
If you have already determined a vector p to be used, then leave out the second line.
  1 件のコメント
Prabha Kumaresan
Prabha Kumaresan 2017 年 12 月 26 日
The following code executes but I am unable to get the random grouping of users.
N_UE=[10 20 30 40 50];
N_SC=[60 70 80 90 100];
for t= 1:length(N_UE)
for r = 1:length(N_SC)
C=rand(N_UE(t),N_SC(r));
s = size(C,1);
p = mod((0:s-1)+randi(s-1,1,s),s)+1; % p(k) never equals k
B = C;
for k = 1:s
B(p(k),k) = B(k,k);
end
end
end

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

Community Treasure Hunt

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

Start Hunting!

Translated by