randomly select elements of an array

Hi How i can select randomly elements from a matrix o array
I have the matrix and i want to select "x" numbers of elements
thanks

 採用された回答

Walter Roberson
Walter Roberson 2012 年 4 月 12 日

11 投票

msize = numel(YourMatrix);
idx = randperm(msize);
YourMatrix(idx(1:x))
If you have a fairly new version of MATLAB, you can instead use
msize = numel(YourMatrix);
YourMatrix(randperm(msize, x))

10 件のコメント

Noe alvarado
Noe alvarado 2012 年 4 月 12 日
thanks. i will try it
Noe alvarado
Noe alvarado 2012 年 4 月 12 日
how i can select randomly "N" rows of a matrix?
for example
M =
[ a12, a12]
[ a21, a22]
[ a31, a32]
[ a41, a42]
For N=2
B =
[ a12, a12]
[ a31, a32]
[ a21, a22]
Sourav Mondal
Sourav Mondal 2012 年 10 月 10 日
>> A=[1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> N=2; >> B=A((1:N),:)
B =
1 2 3
4 5 6
bkshn
bkshn 2014 年 4 月 15 日
Hello Walter Roberson
If I want to find a random element in one row of a matrix, Could you help me How can I do?
For example I want to select a random element in last row of a matrix.
Walter Roberson
Walter Roberson 2014 年 4 月 15 日
YourMatrix(end,randperm(size(YourMatrix,2), 1))
feit feit
feit feit 2020 年 11 月 30 日
Hi Walter Roberson,
I want to select a random element from a column, for example from Column 1 or Column 2 or Column 3. Would you help me with the code?
Thank you!
Walter Roberson
Walter Roberson 2020 年 11 月 30 日
YourMatrix(randi(size(YourMatrix,1)), ColumnNumber)
feit feit
feit feit 2020 年 12 月 2 日
Thank you, Walter!
Jaime Gurrola
Jaime Gurrola 2020 年 12 月 7 日
Hi walter, i want to select a random element from this array, help please
A={'Yellow','Green','Blue','Orange','Black','Grey','Red','Brown','Purple','White'};
Walter Roberson
Walter Roberson 2021 年 9 月 3 日
A{randi(numel(A))}

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

その他の回答 (2 件)

Prasobhkumar P. P.
Prasobhkumar P. P. 2020 年 7 月 28 日
編集済み: Walter Roberson 2021 年 9 月 3 日

0 投票

histHandle= histfit(data,nBins,'normal');
[Val Ind] = max(histHandle(2).YData); %histHandle(2) corresponds to the curve fitted
M = histo18(2).XData(Ind);
Adam Wyatt
Adam Wyatt 2021 年 9 月 3 日
編集済み: Adam Wyatt 2021 年 9 月 3 日

0 投票

Use "randsample"
For array:
Arry = [1 4 5 7 8 9];
Smpl = randsample(Arry, 3)
Smpl = 1×3
9 5 4
For strings:
Arry = ["Yellow" "Green" "Blue" "Orange" "Black" "Grey" "Red" "Brown" "Purple" "White"];
Smpl = randsample(Arry, 2)
Smpl = 1×2 string array
"Purple" "Brown"
How do you want to sample the matrix? If its any element, just convert to array (e.g. Mat(:)). Otherwise you have to apply the function to each row/column separately.

3 件のコメント

Walter Roberson
Walter Roberson 2021 年 9 月 3 日
Note: randsample() is from the Statistics and Machine Learning Toolbox
Piyush Kant
Piyush Kant 2022 年 2 月 7 日
Is there any method/parameter with which we can reproduce same random series so that the different algorithms can be evaluated? I know that way it wont be a random series anymore still the psudorandomness can be helpful for multiple runs.
Steven Lord
Steven Lord 2022 年 2 月 7 日
See the "Control Random Number Generation" topic on this documentation page.
Some older functions that use random numbers internally (like eigs, which generates a starting point at random if one is not given to it) have the ability to specify inputs that avoid the randomness (in the case of eigs by specifying v0 in the options structure or specifying the StartVector name-value pair argument.)

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

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by