Help with calculating median of an array without using built in function?

 채택된 답변

Next step:
n = length(b);
t = (n+1)/2;
md = (b(floor(t))+b(ceil(t)))/2; % <-- median

댓글 수: 2

Thank you so much! What would the steps look like if there were an even number of terms?
Same formula works for odd and even numbers:
1. even, say, n = 10, t = (10+1)/2 = 5.5, floor(t) = 5, ceil(t) = 6,
md = (b(5)+b(6))/2 (average of middle two values)
2. odd, say, n = 13, t = (13+1)/2 = 7, floor(7) = 7, ceil(7) = 7,
md = (b(7)+b(7))/2 = b(7) (center value)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 3월 26일

0 개 추천

use n=numel(A) to get the number of element in A. If n is odd, the median will be b((n+1)/2), if n is even, try to find out.
William
William 2023년 9월 27일

0 개 추천

Heres a quicker way:
sortedvec = sort(vec)
l = length(sortedvec)
x = (l + 1) / 2
median = sortedvec(x)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

질문:

2016년 3월 26일

댓글:

DGM
2023년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by