how to choose matrix columns randomly?

hi all. i have two related matrices (A and B). i want to choose randomly 80% of one matrix's columns (e.g A) and corresponding values from second matrix(B)for example:
suppose that two matrices are like this:
A=[1 2 3 4 5 6 7 8 9 10];
B=[11 12 13 14 15 16 17 18 19 20];
desired matrices are for example like:
C=[1 3 4 5 7 8 9 10]
D=[11 13 14 15 17 18 19 20]
would you help me on this? thank you

4 件のコメント

Stephen23
Stephen23 2015 年 2 月 23 日
How many columns does A have? What is 80% of one column? What is 80% of two columns? How do you define "80% of one matrix's columns" ?
maryam
maryam 2015 年 2 月 23 日
matrices are 11*200. so i need two 11*160 matrices. i want to choose these 160 columns randomly.
the cyclist
the cyclist 2015 年 2 月 23 日
編集済み: the cyclist 2015 年 2 月 23 日
Do you want the 160 to be unique, or possibly repeating? For example, might you choose the 5th column twice?
Also, do you have the Statistics Toolbox? A couple commands there make this a bit cleaner.
maryam
maryam 2015 年 2 月 23 日
no, columns should be unique. yes, i have statistics toolbox,but i haven't work with it yet!

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2015 年 2 月 23 日

1 投票

Use the randperm function.
This might do what you want:
cols = randperm(200, 160);
(Note: Untested code, but it should work.)

3 件のコメント

Star Strider
Star Strider 2015 年 2 月 23 日
With respect to your edited Question, my edited Answer becomes:
A=[1 2 3 4 5 6 7 8 9 10];
B=[11 12 13 14 15 16 17 18 19 20];
cols = randperm(size(A,2), round(0.8*size(A,2)));
producing (for example):
cols =
4 2 9 8 3 7 6 5
and:
C = A(cols);
D = B(cols);
producing:
C =
4 2 9 8 3 7 6 5
D =
14 12 19 18 13 17 16 15
maryam
maryam 2015 年 2 月 23 日
thank you for your answer
Star Strider
Star Strider 2015 年 2 月 23 日
My pleasure!

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2015 年 2 月 23 日

1 投票

As Stephen's questions suggest, you need some more specification.
Possible elements of the solution are
  • randi, randsample, or other commands to select some random indices. (Say that vector of random indices is called idx )
  • Something like either B(:,idx) or B(:,A(idx)) to choose from B directly with the indices, or using values from A that are selected by the indices

1 件のコメント

the cyclist
the cyclist 2015 年 2 月 23 日
idx = randsample(200,160);
C = A(idx);
D = B(idx)'

サインインしてコメントする。

Jos (10584)
Jos (10584) 2015 年 2 月 23 日

0 投票

Exactly 80 vs 20? Or in approximation? And how many columns? I also assume the number of rows in A and B are the same.
A suggestion is that you first draw N columns from A and M columns from B, where N/M is 80/20
Ntotal = 10 ;
N = ceil(.8 * Ntotal) % fixed 80 vs 20
% or
% N = sum(rand(100,1)<0.8)) % roughly
M = Ntotal - N
% select N columns from A and M columns from B
cidxA = randi([1 size(A,2)],1,N)
cidxB = randi([1 size(B,2)],1,M)
% stick them together
OUT = [A(:,cidxA) B(:,cidxB)]
% randomise the order
r = randperm(size(OUT,2))
OUT = OUT(:,r)

1 件のコメント

maryam
maryam 2015 年 2 月 23 日
Dear Jos, i edited my question. it wasn't clear. 80 vs 20 should occur in approximation and in both two matrices.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2015 年 2 月 23 日

コメント済み:

2015 年 2 月 23 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by