How Can I randomly select 20% of my array elements AND save their indices (row,column) ?

3 ビュー (過去 30 日間)
Omar Shehata
Omar Shehata 2017 年 7 月 19 日
コメント済み: Jan 2017 年 7 月 19 日
I have a matrix A with n points.
A = [230,30,40,40,80;40 40 30 80 230;40 40 40 230 80]; n=numel(A);
How can i now randomly select a percentage of the points (example p=20% of n) ?
The output should be a new matrix with these random elements. AND another array that tells me "which" elements(row, column) were selected.
Any help appreciated :)

採用された回答

Jan
Jan 2017 年 7 月 19 日
編集済み: Jan 2017 年 7 月 19 日
A = [230,30,40,40,80;40 40 30 80 230;40 40 40 230 80];
n = numel(A);
Index = randperm(numel(A), ceil(numel(A) * 0.20));
[Xp, Yp] = ind2sub(size(A), Index);
Selected = A(Index);
This chooses 20% of the values randomly and replies the values in Selected and the indices in Xp, Yp.
  2 件のコメント
Omar Shehata
Omar Shehata 2017 年 7 月 19 日
Thank you very much :)
what if I now want to have the "Selected" MATRIX in the same dimension as my data matrix "A". and all data which are not selected could be NaN or 0 then ?
Jan
Jan 2017 年 7 月 19 日
@Omar: What did you try so far?
B = nan(size(A));
B(Index) = Selected;

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

その他の回答 (1 件)

ES
ES 2017 年 7 月 19 日
A = [230,30,40,40,80;40 40 30 80 230;40 40 40 230 80];
n=numel(A);
B=reshape(A, 1, n); %1-D array
p = randperm(n);%random permutation of numbers till n
k = p(1:ceil(numel(p)/8));%Very poor algo for 1st to 20% of elements. You can improve here
C=B(p);%Target Matrix

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by