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

1 vue (au cours des 30 derniers jours)
Stephen john
Stephen john le 27 Août 2022
Déplacé(e) : Voss le 6 Nov 2024
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,:)=[];
  2 commentaires
dpb
dpb le 27 Août 2022
What's wrong with the answer to the same Q? from before?
Stephen john
Stephen john le 27 Août 2022
@dpb That question is not answered Thats why i posted to get clearity

Connectez-vous pour commenter.

Réponses (1)

Arjun
Arjun le 6 Nov 2024
Déplacé(e) : Voss le 6 Nov 2024
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!

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by