フィルターのクリア

How to create NaN randomly based on specific rate in specific column

5 ビュー (過去 30 日間)
amj
amj 2017 年 11 月 17 日
編集済み: amj 2017 年 11 月 17 日
Hi,
I'm going to create NaN values in a data set. I found this basic code, but it is not specify for which column.
X = rand(10,2)
p = randperm(numel(X))
%p1 = randperm(X)
randel = p(1:5)
X(p(1:5)) = NaN
This is my code which is able to set NaN in the second column only: Y = rand(10,2) ratioNan=0.5 Y(rand(size(X(:,1)))<=ratioNan,2)=NaN
This code does not produces the result as expected. For instance, if i want to set the 50% NaN values, sometime it is created more or less than 50% of the records. How to change the code so that i will remove exactly 50% from the record specifically in the second column.

採用された回答

KSSV
KSSV 2017 年 11 月 17 日
X = rand(10,2) ;
idx = randsample(size(X,1),size(X,1)/2) ;
X(idx,:) = NaN
  1 件のコメント
amj
amj 2017 年 11 月 17 日
編集済み: amj 2017 年 11 月 17 日
Dear KSSV,
Thanks a million for your answer. I also just resolved the problem. This is my answer:
x = rand(20,2);
idx = randperm(20);
x(idx(1:5),2) = NaN

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by