Search code examples
matlabmatrixmixing

Mixing columns in matlab


I have a 2x100 matrix. It contains 100 elements from 2 different classes. So each element consists of the value itself and the label with the class it belongs to(1 or 2). I want to mix this data into another 2x100 matrix, where the values stay still connected to their labels.

An example with a 2x5 matrix would be:

 A=[1 2 3 4 5;
    1 2 2 2 1]

After mixing:

A=[2 3 5 1 4;
   2 2 1 1 2]

How can I do this? Thanks!


Solution

  • You can index the entire columns (and randomly change the order using randperm)

    Amix = A( :, randperm(size(A,2)) );
    

    See an example at ideone.