How to delete Index which has 0 Value and replicate the matrix from start

22 views (last 30 days)
Hello Everyone, I hope you are doing well. I have an array named as Dataset. I have replace 0 in for those values which has histogram count less than 50 percent of maximum value.
Now in my new array Dataset Has some Values which are equal to 0; I want to delete 0 in array and replicate the matrix from the start to complete matrix length for example the Dataset has length 137x1.
Batchdata=Dataset;
fig=figure; set(fig,'visible','off');
h=histogram(Batchdata,10000);
sumofbins=max(h.Values);
size_MP=round(50/100*sumofbins);
ValueofHistogram= h.Values;
Bindata=h.Data;
Binedges=h.BinEdges;
Binedges(end) = Inf;
deleted_data_idx = false(size(Bindata));
for i=1: length(ValueofHistogram)
if ValueofHistogram(i)<size_MP;
deleted_data_idx(Bindata >= Binedges(i) & Bindata < Binedges(i+1)) = true;
end
end
close(fig);
Dataset(deleted_data_idx,:)=[];

Answers (1)

Arjun
Arjun on 6 Nov 2024 at 11:18
Moved: Voss on 6 Nov 2024 at 16:35
I see that you want to delete some entries from your dataset and then replicate the matrix again to preserve the original length.
The code provided addresses the task of setting specific entries to zero and subsequently removing them. After eliminating all zero entries, you can utilize MATLAB's ‘repmat’ function to replicate the remaining data multiple times. This replication may result in a matrix that exceeds the original length, so you will need to trim it to match the original matrix size.
Kindly refer to the code below for better understanding:
dataStruct = load('dataset.mat');
Dataset = dataStruct.Dataset;
Batchdata = Dataset;
fig = figure;
set(fig, 'visible', 'off');
% Calculate histogram
h = histogram(Batchdata, 10000);
sumofbins = max(h.Values);
size_MP = round(50/100 * sumofbins);
% Determine which bins have counts less than size_MP
ValueofHistogram = h.Values;
Binedges = h.BinEdges;
Binedges(end) = Inf;
deleted_data_idx = false(size(Batchdata));
% Replace values with zero based on histogram
for i = 1:length(ValueofHistogram)
if ValueofHistogram(i) < size_MP
deleted_data_idx(Batchdata >= Binedges(i) & Batchdata < Binedges(i+1)) = true;
end
end
close(fig);
Batchdata(deleted_data_idx) = 0;
Batchdata(Batchdata == 0) = [];
% Replicate data to match original length
original_length = size(Dataset, 1);
% Calculate how many times to replicate to get sufficient entries
replicated_data = repmat(Batchdata, ceil(original_length / numel(Batchdata)), 1);
% Trim to match the original length
Dataset = replicated_data(1:original_length);
Kindly refer to the documentation of ‘repmat’ for better clarity: https://www.mathworks.com/help/releases/R2022a/matlab/ref/repmat.html
I hope this will help!

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!