Median (Median) Filter Matlab
10 views (last 30 days)
Show older comments
How to write a function of size 3x3 in Matlab without using Matlab's own ready function for the median (median) filter
0 Comments
Answers (1)
KALYAN ACHARJYA
on 14 Jun 2020
Edited: KALYAN ACHARJYA
on 14 Jun 2020
disp('##Input Data')
a=randi([1,10],[4,5]) %Any random data
%% # Using MATLAB Function medfilt2
disp('##Using MATLAB Median inbuilt function');
result_MATLAB=medfilt2(a)
%% # Own Custom Function
disp('##Own Median function');
[r c]=size(a);
a_paded=zeros(size(a)+2);
a_paded(2:end-1,2:end-1)=a;
for i=2:r+1
for j=2:c+1
data=a_paded(i-1:i+1,j-1:j+1);
data=sort(data(:));
result_own(i-1,j-1)=data(round(length(data)/2));
end
end
result_own
%You can avoid loop in later section,please see alternate way
Result:
##Input Data
a =
5 3 5 4 8
10 5 3 8 5
4 1 1 2 5
8 1 8 1 3
##Using MATLAB Median inbuilt function
result_MATLAB =
0 3 3 4 0
3 4 3 5 4
1 4 2 3 2
0 1 1 1 0
##Own Median function
result_own =
0 3 3 4 0
3 4 3 5 4
1 4 2 3 2
0 1 1 1 0
0 Comments
See Also
Categories
Find more on Multirate Signal Processing 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!