How can I delete 10% random Selected Index from array without changing shape in Matlab
7 views (last 30 days)
Show older comments
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) =[] ;
0 Comments
Answers (2)
Arif Hoq
on 28 Feb 2022
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 Comments
DGM
on 28 Feb 2022
Edited: DGM
on 28 Feb 2022
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))
DGM
on 28 Feb 2022
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 Comments
DGM
on 28 Feb 2022
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.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!