How to delete Index which has 0 Value and replicate the matrix from start
22 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Arjun
2024 年 11 月 6 日 11:18
移動済み: Voss
2024 年 11 月 6 日 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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!