Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Randomizing column vector and keeping associated numbers in other columns

Subject: Randomizing column vector and keeping associated numbers in other columns

From: Ulrik Nash

Date: 9 May, 2012 09:25:07

Message: 1 of 8

Hi Everyone,

I have a matrix with two columns, and which might potentially hold many rows (>1000).

What is the most efficient way to randomize the numbers in the first column, while keeping the associated value in the second columns? I am thinking about randperm, but as far as I can see it only allows the randomization of one vector at the time, thus disregarding the associated numbers in other columns...

Subject: Randomizing column vector and keeping associated numbers in other

From: Nasser M. Abbasi

Date: 9 May, 2012 09:54:31

Message: 2 of 8

On 5/9/2012 4:25 AM, Ulrik Nash wrote:
> Hi Everyone,
>
> I have a matrix with two columns, and which might potentially hold
> many rows (>1000).
>
> What is the most efficient way to randomize the numbers in the first column,
>while keeping the associated value in the second columns? I am thinking about
>randperm, but as far as I can see it only allows the randomization of one
>vector at the time, thus disregarding the associated numbers in other columns...

help rand()

--------------------------------------
EDU>> A=[1 78 79 80;
          2 101 103 104
       ];

A(:,1)=rand(2,1)
A(:,1)=rand(2,1)
A(:,1)=rand(2,1)
--------------------------------

A =
     0.2785 78.0000 79.0000 80.0000
     0.5469 101.0000 103.0000 104.0000

A =
     0.9575 78.0000 79.0000 80.0000
     0.9649 101.0000 103.0000 104.0000


A =
     0.1576 78.0000 79.0000 80.0000
     0.9706 101.0000 103.0000 104.0000

---------------------------------------------

--Nasser

Subject: Randomizing column vector and keeping associated numbers in other

From: Ulrik Nash

Date: 9 May, 2012 10:24:09

Message: 3 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jodesn$ihg$1@speranza.aioe.org>...
> On 5/9/2012 4:25 AM, Ulrik Nash wrote:
> > Hi Everyone,
> >
> > I have a matrix with two columns, and which might potentially hold
> > many rows (>1000).
> >
> > What is the most efficient way to randomize the numbers in the first column,
> >while keeping the associated value in the second columns? I am thinking about
> >randperm, but as far as I can see it only allows the randomization of one
> >vector at the time, thus disregarding the associated numbers in other columns...
>
> help rand()
>
> --------------------------------------
> EDU>> A=[1 78 79 80;
> 2 101 103 104
> ];
>
> A(:,1)=rand(2,1)
> A(:,1)=rand(2,1)
> A(:,1)=rand(2,1)
> --------------------------------
>
> A =
> 0.2785 78.0000 79.0000 80.0000
> 0.5469 101.0000 103.0000 104.0000
>
> A =
> 0.9575 78.0000 79.0000 80.0000
> 0.9649 101.0000 103.0000 104.0000
>
>
> A =
> 0.1576 78.0000 79.0000 80.0000
> 0.9706 101.0000 103.0000 104.0000
>
> ---------------------------------------------
>
> --Nasser


Thanks Nasser, but it is not quite what I am after. I think perhaps I should be more specific.

Suppose I have the following matrix:

A = [1 10;2 20;3 30]

What I wish to achieve is randomizing the order of the first columns numbers (ie the 1, 2 and 3), while keeping the associated numbers in columns two. For example:

B = [2 20;1 10; 3 30]

I hope I am clearer now.

Subject: Randomizing column vector and keeping associated numbers in other

From: Nasser M. Abbasi

Date: 9 May, 2012 10:31:11

Message: 4 of 8

On 5/9/2012 5:24 AM, Ulrik Nash wrote:

>
> Thanks Nasser, but it is not quite what I am after.
>I think perhaps I should be more specific.
>
> Suppose I have the following matrix:
>
> A = [1 10;2 20;3 30]
>
> What I wish to achieve is randomizing the order of the first columns
>numbers (ie the 1, 2 and 3), while keeping the associated numbers
>in columns two. For example:
>
> B = [2 20;1 10; 3 30]
>
> I hope I am clearer now.

sorry, I misunderstood.

May be this will help. Did not try it, but quick look shows
it might be what you want:

"SHUFFLE Shuffles vectors or matrices."

http://www.mathworks.com/matlabcentral/fileexchange/3029-shuffleorderby


--Nasser

Subject: Randomizing column vector and keeping associated numbers in other

From: Ulrik Nash

Date: 9 May, 2012 11:11:06

Message: 5 of 8

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jodh1e$o7t$1@speranza.aioe.org>...
> On 5/9/2012 5:24 AM, Ulrik Nash wrote:
>
> >
> > Thanks Nasser, but it is not quite what I am after.
> >I think perhaps I should be more specific.
> >
> > Suppose I have the following matrix:
> >
> > A = [1 10;2 20;3 30]
> >
> > What I wish to achieve is randomizing the order of the first columns
> >numbers (ie the 1, 2 and 3), while keeping the associated numbers
> >in columns two. For example:
> >
> > B = [2 20;1 10; 3 30]
> >
> > I hope I am clearer now.
>
> sorry, I misunderstood.
>
> May be this will help. Did not try it, but quick look shows
> it might be what you want:
>
> "SHUFFLE Shuffles vectors or matrices."
>
> http://www.mathworks.com/matlabcentral/fileexchange/3029-shuffleorderby
>
>
> --Nasser

No problem, and thank you for your help :-)

Subject: Randomizing column vector and keeping associated numbers in other

From: dpb

Date: 9 May, 2012 13:08:47

Message: 6 of 8

On 5/9/2012 4:25 AM, Ulrik Nash wrote:
> Hi Everyone,
>
> I have a matrix with two columns, and which might potentially hold many
> rows (>1000).
>
> What is the most efficient way to randomize the numbers in the first
> column, while keeping the associated value in the second columns? I am
> thinking about randperm, but as far as I can see it only allows the
> randomization of one vector at the time, thus disregarding the
> associated numbers in other columns...


 >> x=[1 10;2 20;3 30];
 >> r=randperm(3)
r =
      1 3 2
 >> [~,idx]=sort(r);
 >> z=[x(r,1) x(idx,2)]
z =
      1 10
      3 30
      2 20
 >>

Perhaps not "most efficient" but straightforward would be...for the size
of vector you mention I think performance wouldn't be at all bad. (yeah,
for a sample test of

x=[1:1000]';
x=[x rand(size(x))];
r=randperm(1000);
[~,idx]=sort(r);
z=[x(r,1) x(idx,2)]

at the command line the response was essentially instantaneous.)

--

Subject: Randomizing column vector and keeping associated numbers in other

From: Roger Stafford

Date: 9 May, 2012 17:28:09

Message: 7 of 8

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <jodgk9$846$1@newscl01ah.mathworks.com>...
> ........
> Suppose I have the following matrix:
>
> A = [1 10;2 20;3 30]
>
> What I wish to achieve is randomizing the order of the first columns numbers (ie the 1, 2 and 3), while keeping the associated numbers in columns two. For example:
>
> B = [2 20;1 10; 3 30]
>
> I hope I am clearer now.
- - - - - - - - -
 B = A(randperm(size(A,1)),:);

  (If I have understood your description correctly, there is no difference in the roles played by column one as against the other columns. You simply want to apply the same random permutation to each column.)

Roger Stafford

Subject: Randomizing column vector and keeping associated numbers in other

From: dpb

Date: 9 May, 2012 17:29:45

Message: 8 of 8

On 5/9/2012 12:28 PM, Roger Stafford wrote:
...

> (If I have understood your description correctly, there is no difference
> in the roles played by column one as against the other columns. You
> simply want to apply the same random permutation to each column.)

DOH! Slaps head...good catch.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us