COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### how to choose random rows from a matrix

• Email
• Follow

Hi! I have a problem here and need some help:
Suppose there's a m*n (m around 10) matrix, how to choose a random 2 rows from the matrix and exhaust all combination? And also to note down which two rows were used for each operation? Thanks in advance!
 0

See related articles to this posting

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hp9cn5\$ped\$1@fred.mathworks.com>...
> Hi! I have a problem here and need some help:
> Suppose there's a m*n (m around 10) matrix, how to choose a random 2 rows from the matrix and exhaust all combination? And also to note down which two rows were used for each operation? Thanks in advance!

There may be a shorter way with a built-in method of Matlab, but this code just works fine, too:
---------------------------------------------------------------------------------------
[m, n] = size(X); % m data points, n dimensions
randomPoints = [];
for i=1:2
index = random('unid', m); % Pick the index at random.
randomPoints(i,:) = X(index,:); % Add random point.
X(index,:) = []; % Delete selected row.
m = m-1;
end
---------------------------------------------------------------------------------------
 0

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hp9cn5\$ped\$1@fred.mathworks.com>...
> Hi! I have a problem here and need some help:
> Suppose there's a m*n (m around 10) matrix, how to choose a random 2 rows from the matrix and exhaust all combination? And also to note down which two rows were used for each operation? Thanks in advance!

a hint:

help randperm;

us
 0

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hp9cn5\$ped\$1@fred.mathworks.com>...
> Hi! I have a problem here and need some help:
> Suppose there's a m*n (m around 10) matrix, how to choose a random 2 rows from the matrix and exhaust all combination? And also to note down which two rows were used for each operation? Thanks in advance!

There may be a shorter way with a built-in method of Matlab, but this code just works fine, too:
---------------------------------------------------------------------------------------
[m, n] = size(X); % m data points, n dimensions
randomPoints = [];
for i=1:2
index = random('unid', m); % Pick the index at random.
randomPoints(i,:) = X(index,:); % Add random point.
X(index,:) = []; % Delete selected row.
m = m-1;
end
---------------------------------------------------------------------------------------
 0

Thanks for the help so far. Is there a way to exhaust all possible combination? my goal is not to just stop at selecting 2 rows, but to try out all combination. It would also be great if no row is deleted during the process. Much appreciated.
 0

just found out how to pick all the combination with two loops. problem now is: suppose each combination goes through an operation and results in an array, how to store them in an indexed way?
 0

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hp9th9\$3ks\$1@fred.mathworks.com>...
> just found out how to pick all the combination with two loops. problem now is: suppose each combination goes through an operation and results in an array, how to store them in an indexed way?

You can use 3-D arrays. For example, if each iteration results in an array and if you want to save them all:

---------------------------------------
AllArrays = [];
for i=1:1:n
AllArrays(:,:,i) = theArray;
end
---------------------------------------
 0

the 3d matrix seems to be banded with lots of zeros at the rim. is there a wise way to solve this or sequence the results in a 2d array instead?
 0

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hpcnos\$n8b\$1@fred.mathworks.com>...
> the 3d matrix seems to be banded with lots of zeros at the rim. is there a wise way to solve this or sequence the results in a 2d array instead?

a hint:

help cell;

us
 0

"Chan Chun K Chan" <asdf@yahoo.com> wrote in message <hp9oni\$1mt\$1@fred.mathworks.com>...
> Thanks for the help so far. Is there a way to exhaust all possible combination? my goal is not to just stop at selecting 2 rows, but to try out all combination. It would also be great if no row is deleted during the process. Much appreciated.

If you want to check all possible combinations, then why are you choosing pairs randomly? Just loop over all possible row-index pairs. Then you can use the loop variable as the index into your output array as well.
 0

Dear Chan Chun K Chan!

> Suppose there's a m*n (m around 10) matrix, how to choose a random 2 rows from the matrix and exhaust all combination? And also to note down which two rows were used for each operation?

m = repmat(1:10, 10, 1);
mT = transpose(m);
Index = [m(:), mT(:)];
Index = Index(randperm(size(Index, 1)), :);

Now Index(i, 1) and Index(i,2) are two different row indices, Index is shuffled randomly and exhausts all combinations.

Are you sure that you want combinations, so a row can be chosen twice?
If you want to avoid repetitions, you could use several tools from the FEX:
VChooseKO http://www.mathworks.com/matlabcentral/fileexchange/26242
or
Combinator http://www.mathworks.com/matlabcentral/fileexchange/24325

E.g.:
Index = VChooseKO(1:m, 2);   % Or VChooseKRO etc
Index = Index(randperm(size(Index, 1)), :);

Good luck, Jan
 0

Just a bit of exploration here, suppose when choosing this combination of two without repeatition, i also want to avoid certain pairs, how should it be done?
 0

Dear Chan!

> Just a bit of exploration here, suppose when choosing this combination of two without repeatition, i also want to avoid certain pairs, how should it be done?

If you have a M = [m x 2] matrix of indices and want to exclude the index pairs in P = [p x 2], use:
exclude = ismember(P, M, 'rows');
M(exclude, :) = [];

Jan
 0