vendredi 13 avril 2018

Shuffling non-zero elements of each column in a matrix in MATLAB

I want to shuffle non-zero elements of each column in a matrix but keep the zero elements at the same place. Like I have this:

A = 
 [10    0     30    40    50    60
  11    0     31    41    0     61
  0     22    32    42    0     62
  13    23    0     43    0     63
  0     24    34    44    54    64
  15    0     35    0     0     65
  16    26    36    46    56    66]

And I want this:

B =
   [13    0     32    44    54    64
    11    0     35    42    0     63
    0     24    36    40    0     61
    16    23    0     43    0     62
    0     22    31    41    56    60
    10    0     30    0     0     66
    15    26    34    46    50    65]

So herein I have the zeros at the exact same place (i.e. ~A = ~B) and the non-zero elements are shuffled. Obviously shuffling the columns using 'randperm' does not work as it does not allow me to keep zeroes at the same place!




Aucun commentaire:

Enregistrer un commentaire