Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Matrix manipulation using reshape
1 回表示 (過去 30 日間)
古いコメントを表示
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
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.
回答 (1 件)
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 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!