Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Matrix manipulation using reshape

1 visualizzazione (ultimi 30 giorni)
Prabha Kumaresan
Prabha Kumaresan il 18 Dic 2017
Chiuso: MATLAB Answer Bot il 20 Ago 2021
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 Commenti
Roger Stafford
Roger Stafford il 18 Dic 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.
Prabha Kumaresan
Prabha Kumaresan il 18 Dic 2017
I need to have random grouping of rows which reults in sharing their values.

Risposte (1)

Roger Stafford
Roger Stafford il 18 Dic 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 Commento
Prabha Kumaresan
Prabha Kumaresan il 26 Dic 2017
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

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by