Clear Filters
Clear Filters

I want to delete 5% random Selected Index from array and replace zero at the end MATLAB

2 views (last 30 days)
hello everyone i hope you are doing well
i have dataset of shape 1x1000, i have implemeneted the following code to delete 5% samples randomly
but output replace only first index value is saved
How can i do it in MATLAB
Please help
1x1000 but value is not saving in output
output matrix =[200 0 0 0 .......]
load('dataset1')
N = numel(dataset1) ;
percentageMP=5;
size_MP=round(percentageMP/100*N);
MPV=zeros(size(dataset1));
for i=1:length(size_MP)
MP = randsample(N,size_MP) ;
sortvalue=sort(MP);
end
Temp_series1=zeros(size(dataset1));
index=1
totallength=length(dataset1)-length(MP)
for j=1:length(totallength)
for k=1:length(MP)
if j==MPV(k)
index=index+1;
end
end
Temp_series1(j)=dataset1(index)
end

Accepted Answer

Matt J
Matt J on 1 Mar 2022
Edited: Matt J on 1 Mar 2022
load('dataset1')
N = numel(dataset1) ;
percentageMP=5;
size_MP=round(percentageMP/100*N);
discard=randperm(N,size_MP);
dataset1(discard)=[];
dataset1(end+1:N)=0;
  34 Comments
Walter Roberson
Walter Roberson on 2 Mar 2022
"what the effect of randomsample instead of randperm?"
The code in randsample() was designed before Mathworks upgraded the internal randperm algorithm for the two-input case. Because of that, it has an internal "optimization" for the case where less than 1/4 of the values are being selected, with the "optimization" being based on using randi() until enough distinct random values have been generated and then randomizing their order using randperm. This is guaranteed to require at least twice as many random number generations as would be used for a Fisher-Yates shuffle, which is what randperm would use for this configuration.
Also randsample requires the Statistics Toolbox but randperm does not.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!