Select elements from only three consecutive columns.

1 回表示 (過去 30 日間)
AM-Laurentian
AM-Laurentian 2019 年 4 月 4 日
回答済み: AM-Laurentian 2019 年 4 月 5 日
Hello all,
I have a 12 by 12 matrix, and I want to select 10 random elements. The elements should be from only three consecutive columns.
For example, I want to use 10 random elements from columns (1, 2, 3) or they could be from columns (2, 3, 4) or columns(3, 4, 5) and so on.
Any help please.
Thank you
  2 件のコメント
Guillaume
Guillaume 2019 年 4 月 5 日
編集済み: Guillaume 2019 年 4 月 5 日
Please do not ask the same question twice.
Oh, and it looks like you delete your question once answered. That's not going to go down very well with us.
AM-Laurentian
AM-Laurentian 2019 年 4 月 5 日
I am really sorry, it was a mistake. I realized that the question was submitted twice, I tried to delete it as soon as it was submitted but I could not. I apologies for any inconvenience.

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

採用された回答

Guillaume
Guillaume 2019 年 4 月 5 日
編集済み: Guillaume 2019 年 4 月 5 日
startcolumn = randi(size(yourmatrix, 2) - 2); %select 1st of 3 consecutive column at random
candidateelements = yourmatrix(:, startcolumn : startcolumn + 2); %elements from the 3 consecutive columns
selectedelements = candidateelements(randperm(numel(candidateelements), 10)); %take 10 of these at random
  2 件のコメント
AM-Laurentian
AM-Laurentian 2019 年 4 月 5 日
Thank you for your help,
I do not know if I still can ask you for more scenarios of the same question, please!!
Let say that I have a 144 by 3 matrix (there is a small example below) the 1st column contains my integer elements, the second and 3rd columns are X and Y indices. You can see that for IX I have (5,10,15,20,25,30). I want to select 10 random elements but they should be from three consecutive X indices. Let say (5, 10, 15) or (15, 20, 25) etc. Thanks in advance
E IX IY
1 5 5
2 5 10
3 5 15
4 5 20
5 5 25
6 5 30
7 10 5
8 10 10
9 10 15
10 10 20
11 10 25
12 10 30
13 15 5
14 15 10
15 15 15
16 15 20
17 15 25
18 15 30
19 20 5
20 20 10
21 20 15
22 20 20
23 20 25
24 20 30
25 25 5
26 25 10
27 25 15
28 25 20
29 25 25
30 25 30
32 30 10
33 30 15
34 30 20
35 30 25
36 30 30
Guillaume
Guillaume 2019 年 4 月 5 日
For that, it would have been better to start a new question as this is a completely different scenario.
[ux, ~, rows] = unique(yourmatrix(:, 2)); %get unique values and matching rows
startx = randi(numel(ux) - 2); %start index of 3 consecutive x
rowpool = find(ismember(rows, startx:startx + 2)); %row index of all rows within selected 3 consecutive x
selectedrows = rowpool(ranpderm(numel(rowpool, 10))); %take 10 rows at random out of the pool

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

その他の回答 (1 件)

AM-Laurentian
AM-Laurentian 2019 年 4 月 5 日
Thank you so much Guillaume; It is very helpful.

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by