Sign in

username:

password:



Not a member?

Search matlab



Search tips

Subscribe to matlab



matlab by Keywords

Atanh | Autocorrelation | Bandpass Filter | C++ | Conv | Database | Deconv | Excel | FFT | Filter | Filtering | FIR | Fourier Transfrom | FSK | Gaussian Noise | Haykin | IFFT | Image | Java | LFSR | LMS | LPC | MEX | OFDM | QPSK | Radix | Random | Sampling | Segmentation | Simulink | Visual Basic | Waveform | Wavelet

Ads

Discussion Groups

Discussion Groups | Matlab DSP | how can we select a random element of a matrix

Technical discussion about Matlab and issues related to Digital Signal Processing.

  

Post a new Thread

how can we select a random element of a matrix - cagi...@hotmail.com - May 30 9:18:57 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,



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

RE: how can we select a random element of a matrix - Amit Shaw - Jun 4 8:14:04 2007

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...@yahoogroups.com [mailto:m...@yahoogroups.com] On Behalf
Of c...@hotmail.com
Sent: Wednesday, May 30, 2007 1:29 PM
To: m...@yahoogroups.com
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.
**********************************************************************


(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - Amit Pathania - Jun 4 8:14:07 2007

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 <a...@yahoo.com> 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    



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - dutmatlab - Jun 4 8:14:28 2007

Hi,

try this :

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

idx=randperm(numel(M));

M(idx(1:3))

Jérôme



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - Vikram Rana - Jun 4 8:14:43 2007

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<nos<4)
and 4 random nos. for column(1<nos<5)

and display

peace
vicks

--- In m...@yahoogroups.com, 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 need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - sjoh...@cnbc.cmu.edu - Jun 4 8:15:05 2007

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,
>



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - PRAJIT S NAIR - Jun 4 8:15:18 2007

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 <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,
>  
>



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - Amit Pathania - Jun 4 8:15:48 2007

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



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - nirup reddy - Jun 4 8:16:40 2007

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 <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,
>  
>



(You need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )

Re: how can we select a random element of a matrix - entave - Jun 4 8:17:47 2007

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...@yahoogroups.com, 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 need to be a member of matlab -- send a blank email to matlab-subscribe@yahoogroups.com )