Azzera filtri
Azzera filtri

How can i replace a number with its no of ocurrences

2 visualizzazioni (ultimi 30 giorni)
i have a matrix=a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1] i want to replace ones by their no of occurences in each row like a=[1 0 2 0 3;0 1 2 3 4;1 2 0 3 4;1 2 3 4 5]

Risposta accettata

Mohammad Abouali
Mohammad Abouali il 28 Mag 2015
Modificato: Mohammad Abouali il 28 Mag 2015
a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1];
cumsum(a,2).*a
ans =
1 0 2 0 3
0 1 2 3 4
1 2 0 3 4
1 2 3 4 5
  2 Commenti
Ayesha Maroof
Ayesha Maroof il 28 Mag 2015
thanx alot Mohammad Abouali.i lilttle more help needed.can i shuffle the values now.that zeros remain and its place but rest values shuffles with in a row like [3 0 1 0 2;0 2 1 4 3...] like this
Mohammad Abouali
Mohammad Abouali il 28 Mag 2015
Modificato: Mohammad Abouali il 28 Mag 2015
a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1];
b=cumsum(a,2).*a;
disp('b:')
disp(b)
will print:
b:
1 0 2 0 3
0 1 2 3 4
1 2 0 3 4
1 2 3 4 5
Now shuffling:
shuffle=@(data) data(randperm(numel(data)));
for row=1:size(a,1)
mask=logical(a(row,:));
b(row,mask)= shuffle(b(row,mask));
end
disp('shuffled b:')
disp(b)
will print: (well it is random, so each time prints something different)
shuffled b:
2 0 3 0 1
0 1 3 4 2
1 3 0 2 4
4 5 3 1 2

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by