randomly select elements of an array
1,006 ビュー (過去 30 日間)
表示 古いコメント
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
0 件のコメント
採用された回答
Walter Roberson
2012 年 4 月 12 日
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))
その他の回答 (2 件)
Prasobhkumar P. P.
2020 年 7 月 28 日
編集済み: Walter Roberson
2021 年 9 月 3 日
histHandle= histfit(data,nBins,'normal');
[Val Ind] = max(histHandle(2).YData); %histHandle(2) corresponds to the curve fitted
M = histo18(2).XData(Ind);
0 件のコメント
Adam Wyatt
2021 年 9 月 3 日
編集済み: Adam Wyatt
2021 年 9 月 3 日
Use "randsample"
For array:
Arry = [1 4 5 7 8 9];
Smpl = randsample(Arry, 3)
For strings:
Arry = ["Yellow" "Green" "Blue" "Orange" "Black" "Grey" "Red" "Brown" "Purple" "White"];
Smpl = randsample(Arry, 2)
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 件のコメント
Steven Lord
2022 年 2 月 7 日
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.)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!