How to remove extra value from histogram In MATLAB

22 views (last 30 days)
Hello everyone, I hope you are doing well.
I have the following dataset in which i have a pattern, there are some values which are the outliers or you can say the missing values which occur in different place. i want to remove the values using histogram.
i have compute the histogram of the data as you can see in image untitled.jpg. There are three values 4800 5130 5540 which have histogram value of 322, 317 and 312 while the other have value less then 50.
I want to keep the Values and Indexes of 50% (in above case 161) of maximum value of histogram and remove the remaining values.
I have write the following code. But it just return a single value not the original matrix (4800 5130 5540)
Can any body help me in that Please
h=histogram(Values)
sumofbins=max(h.Values);
size_MP=round(50/100*sumofbins);
ValueofHistogram= h.Values;
Bindata=h.Data
for i=1: length(ValueofHistogram)
if ValueofHistogram(i)<size_MP;
Bindata(i)=0;
end
end

Answers (2)

Voss
Voss on 17 Apr 2022
You can't change the 'Values' property of a histogram directly, but you can change its underlying 'Data'. In this case, you can remove data from within those bins whose Value is less than half the maximum Value:
load('His.mat')
h=histogram(Values)
h =
Histogram with properties: Data: [4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 10340 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 5540 4800 5130 10340 5130 … ] Values: [312 317 322 0 0 0 0 0 0 0 18 17 10 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1] NumBins: 34 BinEdges: [4500 5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 10500 11000 11500 12000 12500 13000 13500 14000 14500 15000 15500 16000 16500 17000 17500 18000 18500 19000 19500 20000 20500 21000 21500] BinWidth: 500 BinLimits: [4500 21500] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Show all properties
sumofbins=max(h.Values);
size_MP=round(50/100*sumofbins);
ValueofHistogram= h.Values;
Bindata=h.Data;
Binedges=h.BinEdges;
Binedges(end) = Inf;
for i=1: length(ValueofHistogram)
if ValueofHistogram(i)<size_MP;
Bindata(Bindata >= Binedges(i) & Bindata < Binedges(i+1)) = [];
end
end
xl = xlim();
h.Data = Bindata;
xlim(xl); % restore axes xlim, if you want to
  12 Comments
Image Analyst
Image Analyst on 19 Apr 2022
Try my well commented last comment below, at the end of my answer. I think it will do what you want now. It gives you the indexes in your original data where the counts are less than 50% of the max count. It then uses those indexes to delete those infrequently occurring data from the original data set.

Sign in to comment.


Image Analyst
Image Analyst on 17 Apr 2022
Try this:
s = load('his.mat')
data = s.Values;
maxValue = max(data)
brightData = data(data >= 0.5 * maxValue)
histogram(brightData);
grid on;
  9 Comments
Image Analyst
Image Analyst on 19 Apr 2022
I think it will do what you want now. It gives you the indexes in your original data where the counts are less than 50% of the max count. It then uses those indexes to delete those infrequently occurring data from the original data set
load('His.mat')
uniqueValues = unique(Values)
whos Values
subplot(2, 1, 1);
histObject = histogram(Values, 'BinEdges', uniqueValues)
grid on;
% "I want to keep the Values and Indexes of 50% (in above case 161) of maximum value of histogram
% and remove the remaining values."
maxBinCounts = max(histObject.Values)
% Find out which bins have fewer counts than 50% of the max bin count in them.
bins50 = find(histObject.Values <= 0.50 * maxBinCounts)
indexesToDelete = false(1, length(Values)); % Array to keep track of what values to delete.
% Delete the values from the original data if they are in the bin with less than 50
for k = 1 : length(bins50)
thisIndex = bins50(k);
% Get values included in this histogram bin.
lowValue = histObject.BinEdges(thisIndex);
highValue = histObject.BinEdges(thisIndex+1);
% Find indexes of original data where these values lie.
theseIndexes = (Values >= lowValue) & (Values < highValue);
% Mark for deletion.
indexesToDelete(theseIndexes) = true;
end
% Delete the elements. theseIndexes are the indexes of the lower count values in the original data set.
% By the way it's confusing to call your data "Values" because the histogram object calls
% them "Data" and has another variable for "Values" which is the counts in the bins.
% I'd recommend you call your original data "Data" instead of "Values" to avoid confusion.
Values(indexesToDelete) = [];
whos Values
subplot(2, 1, 2);
histObject = histogram(Values, 'BinEdges', uniqueValues)
grid on;

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by