How can I delete 10% random Selected Index from array without changing shape in Matlab

13 ビュー (過去 30 日間)
Med Future
Med Future 2022 年 2 月 28 日
コメント済み: Med Future 2022 年 3 月 1 日
Hello everyone, I hope you are doing well.
I have the dataset of 250x1000
I want to deleted 10% random Selected Index from array and shape will be like as input dataset. I have impleted the following code But it changes the dataset shape
Please can anybody help me in that
dataset1=dataset;
N = numel(dataset1) ; % total number of elements
idx = randsample(N,round(10/100*N)) ; % select 10% of indices randomly
dataset1(idx) =[] ;

回答 (2 件)

Arif Hoq
Arif Hoq 2022 年 2 月 28 日
try this:
A=load('datasetvalue.mat');
AA=A.dataset ;
N = numel(AA) ; % total number of elements
tenpercent=round(10/100*N); % 10 percent of total elements
array=randi(10,250,100); % 10 percent random data
[idx]=find(array>0);
AA(idx)=[];
output=reshape(AA,250,[]);
  7 件のコメント
DGM
DGM 2022 年 2 月 28 日
編集済み: DGM 2022 年 2 月 28 日
Note that this only deletes the first 10% of indices, not a random sample of indices.
Also, since the array is reshaped, there is no longer any correspondence between elements across rows.
To replace (any) random element with (e.g.) NaN:
A=load('datasetvalue.mat');
AA=A.dataset;
N = numel(AA); % total number of elements
% doing it this way makes sure the indices are unique
% otherwise you'll end up with <10% being replaced
idx = 1:N;
idx = idx(randperm(N));
AA(idx(1:round(0.1*N))) = NaN;
nnz(isnan(AA))
Med Future
Med Future 2022 年 3 月 1 日
@DGM Okay if we delete random sample then append zero at the end to comple 1000 sample per row how can i do that?

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


DGM
DGM 2022 年 2 月 28 日
Numeric arrays cannot have "holes" in them. If you want to omit data, you either have to replace it with some value that prevents it from being influencing your process (e.g. 0 or NaN), or you need to adapt your process to address only the valid parts of your array (e.g. using a logical mask).
  2 件のコメント
Med Future
Med Future 2022 年 2 月 28 日
@DGM then How this problem should be solved?
DGM
DGM 2022 年 2 月 28 日
That depends on what you want, the meaning of your data, and how you intend to process it.
If you want the array to stay 250x1000, then you'll need to either replace the elements as mentioned or alter the process to address only the relevant elements.
If your data is essentially tabular, where each row represents a set of corresponding values, then omitting any one element in the row may make it unusable. So then, would the goal be to remove 10% of rows?
Since I don't know what your data means, or what you're doing with it, or why you need to omit values, I don't know what you need to do.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by