How can I delete 10% random Selected Index from array without changing shape in Matlab
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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
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))
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 个评论
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 Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!