Clear Filters
Clear Filters

Creating a function to calculate median

29 views (last 30 days)
Hey there,
I have been tasked with creating a function that calculates the median number of an array without using the inbuilt median function.
% Function to calculate the median of an array
function out_val = my_median(in_val);
sorted = sort(in_val); % sort the array
median = numel(sorted); % find the number of elements in the array
if mod(median, 2) == 0
ans = 1
ans = 0
if ans = 1
% Code to run when median is even, meaning get two numbers and divide by two to find median
num = (median + 1) / 2
out_val = % either of the above
The question I'm asking is probably more maths related by how do I go about calculating the median number if the number of elements is even?
Also, is my code efficient enough or is there something that could be improved?
Thanks in advance

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Apr 2015
You may want to consider renaming your variable median which isn't the "median" but the number of elements in your sorted array of numbers. You can then use this value to determine what to do next - if even do something, if odd do another and avoid use of ans
sortedArray = sort(in_val);
numElems = numel(sortedArray);
if mod(numElems,2) == 0
% even number of elements
% odd number of elements
If is is an odd number of elements, you know what to do already - take numElems and add one to it and divide by two to get the index of the median element in your list
medianIdx = (numElems + 1)/2;
For the even case, you already have a comment that (almost) correctly indicates what you are supposed to do - take the average of the two middle numbers whose indices would be
medianIdx1 = numElems/2;
medianIdx2 = medianIdx + 1;
Since this is a homework question, I will leave the rest to you which is to use the index or indices to determine the median value from your sortedArray.
You may also want to consider naming your input and output variables to what they are rather than just the generically named in_val and out_val. The former is an array of numbers and the latter is the median, so use these descriptions when naming your variables.

More Answers (1)

kokeb Dese
kokeb Dese on 31 Jul 2018
witching Bilateral Filter Noise Removal matlab code Here we discuss about Switching Bilateral Filter Noise Removal step by step.
1.Read the image pixel by pixel (i,j). 2. Each pixel we construct 4 sub windows.
For a (2N+1) (2N+1) window we divide the window into four (N+1)(N+1) subwindows .
case N = 2
3.Find the Sorted Quadrant Median Vector (SQMV) for all subwindows.
SQM1, SQM2, SQM3 and SQM4 are the medians
4. Then we find the regions from above all SQM.
Uniform Region Diagonal edge in dark side Diagonal edge in dark side Veritical edge Horizontal edge Diagonal line Gradual chage edge Texture
5. Then we find the reference median (SQMR) based on above regions. 6. Then apply Bilateral Filter. 7. The apply noise detection .
clc clear all close all close all hidden warning off
con = 1; % con1 for
img = imread('lena.jpg');
figure;imshow(img); title('Input Image');
img = imnoise(img,'salt & pepper',0.01); % salt and pepper Tk1 = 30; % for salt and paper noise.. Tk2 = 15; % for salt and paper noise..
% img = imnoise(img,'gaussian',0,0.01); % gaussian noise % Tk1 = 25; % for Gaussian noise.. % Tk2 = 5; % for Gaussian noise..
img = double(img); figure;imshow(uint8(img)); title('Noisy Image');
ext_filt = img; sigma_R = 40; [r c] = size(img); N = 2; pi_val = 25; h = waitbar(0,'Applying Switching bilateral filter..'); L = 1; for i = (N+1):r-(N+1) waitbar(L/length((N+1):r-(N+1))); L = L+1; for j = (N+1):c-(N+1) [m0,m1,m2,m3,m4] = meadian_calc(i,j,img,N); dav = dav_valc(m1,m2,m3,m4,img,i,j); SQMDB = m4-m1; SQMDC = m3-m2; SQMR = SQMR_calc(SQMDC,m1,m2,m3,m4,dav,pi_val); [s1 s2] = find_S1_S2(img,i,j,SQMR,Tk1,Tk2); if(dav~=0) sigma_S = 3; else sigma_S = 1; end f_x_y = f_of_x_y(i,j,N,img,sigma_S,sigma_R,s2,SQMR); ext_filt(i,j) = f_x_y; end end close(h);
figure; imshow(uint8(ext_filt));%output...
Referencce : Switching Bilateral Filter With a Texture/Noise Detector for Universal Noise Removal Chih-Hsing Lin, Jia-Shiuan Tsai, and Ching-Te Chiu Transactions on: Image Processing, IEEE Journals 2010 what is the code for meadian_calc(), dav_valc() and SQMR_calc() source codes. please help me!

Community Treasure Hunt

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

Start Hunting!