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 CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by