# How to remove extra value from histogram In MATLAB

17 views (last 30 days)
Med Future on 17 Apr 2022
Commented: Image Analyst on 19 Apr 2022
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
Med Future on 17 Apr 2022

Image Analyst on 17 Apr 2022
Try this:
data = s.Values;
maxValue = max(data)
brightData = data(data >= 0.5 * maxValue)
histogram(brightData);
grid on;
Image Analyst on 18 Apr 2022
I almost understand now. You want to remove values that fall into bins with more than 50 counts in them. But what if a value does not occur more than 50 times but is in the bin. For example lets say that you have 312 instances of 4800 and 9 instances of 4801, and your bin includes values from 4800 to 4900 inclusive. Do you want the 9 instances of 4801 removed from the data also? If so, this code will do it:
uniqueValues = unique(Values)
whos Values
histObject = histogram(Values, 'BinEdges', uniqueValues)
grid on;
% Find out which bins have more than 50 counts in them.
bins50 = find(histObject.Values >= 50)
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 more 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
Values(indexesToDelete) = [];
whos Values
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
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;

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:
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
Med Future on 19 Apr 2022
@_ let me explain it to you The count values of 4340 is very much less it does not make shape/pattern which i want to detect. the value 4340 make a extra pattern which has not complete shape just some points. is there any method in which i can remove the value which have less number of counts
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.

### Categories

Find more on Image Category Classification in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!