How to remove extra value from histogram In MATLAB
9 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Image Analyst
2022-4-17
Try this:
s = load('his.mat')
data = s.Values;
maxValue = max(data)
brightData = data(data >= 0.5 * maxValue)
histogram(brightData);
grid on;
9 个评论
Image Analyst
2022-4-18
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:
load('His.mat')
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
2022-4-19
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;
更多回答(1 个)
Voss
2022-4-17
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)
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 个评论
Image Analyst
2022-4-19
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!