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

6 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2022-3-1
编辑:Matt J 2022-3-1
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 个评论
Walter Roberson
Walter Roberson 2022-3-2
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by