Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Matrix manipulation using reshape
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Roger Stafford
le 18 Déc 2017
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.
Réponses (1)
Roger Stafford
le 18 Déc 2017
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 commentaire
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!