Forums

how can we select a random element of a matrix

Started by cagi...@hotmail.com May 30, 2007
Hi all,

How can we select random elements from a matrix. Let suppose, we have a 3x4 matrix,
2 4 6 3
6 8 4 2
2 5 6 7
we want to randomly select 3 elements form that matrix, it doesn't matter from which column or row. What matlab code should we write?

Thank you,
row_index = round(rand(3) * 3);

col_index = round(rand(3) * 4);

x(row_indxe,col_index) will select random elements from the matrix

Thanks and Regards,

Amit Shaw

From: m... [mailto:m...] On Behalf
Of c...@hotmail.com
Sent: Wednesday, May 30, 2007 1:29 PM
To: m...
Subject: [matlab] how can we select a random element of a matrix

Hi all,

How can we select random elements from a matrix. Let suppose, we have a
3x4 matrix,
2 4 6 3
6 8 4 2
2 5 6 7
we want to randomly select 3 elements form that matrix, it doesn't
matter from which column or row. What matlab code should we write?

Thank you,

**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
h...@ittiam.com.
**********************************************************************
ok i tried running it in matlab doesnt work as desired. I dont want to use a for loop for this otherwise this will work .
once the temp matrix is created use a loop

for j= 1:N

result(j) = test_matrix(temp(j,1),temp(j,2));
end

result

Amit Pathania wrote:
I dont have a Matlab running on this computer but I think this should work

N = 3; % How many numbers to select

[nrows, ncols] = size(test_matrix); % this is your matrix which u want to extract numbers from
temp = [randint(N,1,[1,nrows]), randint(N,1,[1,ncols]);

result = test_matrix(temp); % should give the result in a column vector

c...@hotmail.com wrote:

Hi all,

How can we select random elements from a matrix. Let suppose, we have a 3x4 matrix,
2 4 6 3
6 8 4 2
2 5 6 7
we want to randomly select 3 elements form that matrix, it doesn't matter from which column or row. What matlab code should we write?

Thank you,

Amit Pathania
Hi,

try this :

M=[2 4 6 3
6 8 4 2
2 5 6 7]

idx=randperm(numel(M));

M(idx(1:3))

Je
just generate the row and column addresses randomly using
inbuilt functions like rand and randint etc and use them
to pick the elements.

generate 3 random nos. for rows(1 and 4 random nos. for column(1
and display

peace
vicks

--- In m..., cagin_kandemir@... wrote:
> Hi all,
>
> How can we select random elements from a matrix. Let suppose, we
have a 3x4 matrix,
> 2 4 6 3
> 6 8 4 2
> 2 5 6 7
> we want to randomly select 3 elements form that matrix, it doesn't
matter from which column or row. What matlab code should we write?
>
> Thank you,
>
You can do this using randperm -
m = [2 4 6 3;6 8 4 2;2 5 6 7]
z= randperm(numel(m)); % this well generate random arrangement of indices
of m
m(z(1:3)) will be 3 random elements of m.

Hope this helps.

~Sindy

> Hi all,
>
> How can we select random elements from a matrix. Let suppose, we have a
> 3x4 matrix,
> 2 4 6 3
> 6 8 4 2
> 2 5 6 7
> we want to randomly select 3 elements form that matrix, it doesn't matter
> from which column or row. What matlab code should we write?
>
> Thank you,
>
Dear Cagin Kandemir

You can try this :

- x=[2 4 6 3;6 8 4 2; 2 5 6 7];
- y=reshape(x,1,12);
- output=randsrc(1,3,y);

With Warm Wishes
Prajit
On 5/30/07, c...@hotmail.com wrote:
> Hi all,
>
> How can we select random elements from a matrix. Let suppose, we have a
> 3x4 matrix,
> 2 4 6 3
> 6 8 4 2
> 2 5 6 7
> we want to randomly select 3 elements form that matrix, it doesn't matter
> from which column or row. What matlab code should we write?
>
> Thank you,
>
>
I dont have a Matlab running on this computer but I think this should work

N = 3; % How many numbers to select

[nrows, ncols] = size(test_matrix); % this is your matrix which u want to extract numbers from
temp = [randint(N,1,[1,nrows]), randint(N,1,[1,ncols]);

result = test_matrix(temp); % should give the result in a column vector

c...@hotmail.com wrote:

Hi all,

How can we select random elements from a matrix. Let suppose, we have a 3x4 matrix,
2 4 6 3
6 8 4 2
2 5 6 7
we want to randomly select 3 elements form that matrix, it doesn't matter from which column or row. What matlab code should we write?

Thank you,

Amit Pathania
Hi Kandemir
I think a simple solution would be to use rand funtion and round it to
the nearest

row=[ round(rand(1)*size(a,1)) round(rand(1)*size(a,2)) ]
where a is the matrix
a= [2 4 6 3;
6 8 4 2;
2 5 6 7]

u can do it even in 1D for picking 1 element at a time
eg: b=a(:);
round(rand(1)*size(a,1))
or my personal best algo... for getting all the elements in random order

temp_rand=rand(1,size(a(:),1));
[temp_sort,sort_indices]=sort(temp_rand);
use sort_indices to pick elements from a
a(sort_indices);
enjoy..........
Thanks and Regards
Nirup Reddy

On 5/30/07, c...@hotmail.com wrote:
> Hi all,
>
> How can we select random elements from a matrix. Let suppose, we have a
> 3x4 matrix,
> 2 4 6 3
> 6 8 4 2
> 2 5 6 7
> we want to randomly select 3 elements form that matrix, it doesn't matter
> from which column or row. What matlab code should we write?
>
> Thank you,
>
>
Hi cagin, I can say one simple way to do it but not the best way I
think.

You can reach the 2 dimension array element alternatively with 1
argument I mean

a =[ 2 4 6 3
6 8 4 2
2 5 6 7 ];

{ a(2,2) = a(5) = 8 }

So we can use any random command I prefer simple randperm

randperm(size(a,1)*size(a,2)) creates an array contains numbers 1 to
(size of a) with random arrangement. So we can use first three elements
(or other three) as a argument of a.

Complete Code

a = [Your array];
b = randperm(size(a,1)*size(a,2));
firstrandomelement = a(b(1));
secondrandomelement = a(b(2));
thirdrandomelement = a(b(3));

randperm rearranges the array for every run of code, so 3 random
elements change at every run.
I hope this will help
Engin
--- In m..., cagin_kandemir@... wrote:
> Hi all,
>
> How can we select random elements from a matrix. Let suppose, we have
a 3x4 matrix,
> 2 4 6 3
> 6 8 4 2
> 2 5 6 7
> we want to randomly select 3 elements form that matrix, it doesn't
matter from which column or row. What matlab code should we write?
>
> Thank you,
>