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:
generate a matrix with randperm

Subject: generate a matrix with randperm

From: Wendy

Date: 12 Dec, 2010 20:22:04

Message: 1 of 6

Hi all,

I want to generate a matrix of random indices with integers in each column, say 1:5. For example, I want t the output to be

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

Right now, I am generating the random indices by using randperm for column by column. I am wondering if there is a way that I can generate the matrix with a specified number of columns (e.g. 3 in this example).

Thank you very much,
Wendy

Subject: generate a matrix with randperm

From: Yi Cao

Date: 12 Dec, 2010 20:38:06

Message: 2 of 6

"Wendy " <wlq121@gmail.com> wrote in message <ie3atc$730$1@fred.mathworks.com>...
> Hi all,
>
> I want to generate a matrix of random indices with integers in each column, say 1:5. For example, I want t the output to be
>
> output = [
> 1 5 1
> 3 2 4
> 2 1 3
> 5 4 5
> 4 3 2];
>
> Right now, I am generating the random indices by using randperm for column by column. I am wondering if there is a way that I can generate the matrix with a specified number of columns (e.g. 3 in this example).
>
> Thank you very much,
> Wendy

try

[~,output] = sort(rand(5,3));

HTH
Yi

Subject: generate a matrix with randperm

From: Bruno Luong

Date: 12 Dec, 2010 20:39:04

Message: 3 of 6

 [~, A] = sort(rand(5,3))

Bruno

Subject: generate a matrix with randperm

From: Wendy

Date: 12 Dec, 2010 20:52:05

Message: 4 of 6

Yes, it worked. Thanks so much.

Wendy


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ie3bt8$lsp$1@fred.mathworks.com>...
> [~, A] = sort(rand(5,3))
>
> Bruno

Subject: generate a matrix with randperm

From: Wendy

Date: 12 Dec, 2010 21:10:21

Message: 5 of 6

Hi all,

Sorry, I have one more question. If I have an the original matrix with values and the order of the elements in each column. For example,

test_input =

    81 10 16
    91 28 97
    13 55 96
    91 96 49
    63 96 80

test_order =

     5 2 5
     2 1 4
     1 3 1
     3 4 3
     4 5 2

I want to rearrange the elements in each column of 'test_input' according to the order in the 'test_order', so the output that I am looking for is

test_output =
63 28 80
91 10 49
81 55 16
13 96 96
91 96 97

I tried 'test_input(test_order)', but it did not work. Is there a column function that I can use here. Thank you very much.

Wendy

"Wendy " <wlq121@gmail.com> wrote in message <ie3cll$3t1$1@fred.mathworks.com>...
> Yes, it worked. Thanks so much.
>
> Wendy
>
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ie3bt8$lsp$1@fred.mathworks.com>...
> > [~, A] = sort(rand(5,3))
> >
> > Bruno

Subject: generate a matrix with randperm

From: Bruno Luong

Date: 12 Dec, 2010 22:16:05

Message: 6 of 6

>> [m n] = size(test_order)
>> test_output = test_input(sub2ind([m n], test_order, repmat(1:n,m,1)))

test_output =

    63 28 80
    91 10 49
    81 55 16
    13 96 96
    91 96 97

% Bruno

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