Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Matrix manipulation using reshape
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Roger Stafford
am 18 Dez. 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.
Antworten (1)
Roger Stafford
am 18 Dez. 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 Kommentar
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!