# How can I calculate the "Median" of floating point numbers

4 views (last 30 days)
Jim McIntyre on 9 Jun 2023
Commented: James Tursa on 9 Jun 2023
I have three two-dimensional arrays containing floating point numbers. I would like to construct a two-dimensional array that contains the "median" or middle value (not the mean) of the three arrays.
For example, if one element of the arrays contained 4.81, 3.54, and 3.56, I'd like to return 3.56, which is the middle value.
How can I do that?
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

James Tursa on 9 Jun 2023
E.g.,
x = randi(10,2,3)
x = 2×3
8 4 8 2 8 4
y = randi(10,2,3)
y = 2×3
10 5 5 6 5 8
z = randi(10,2,3)
z = 2×3
9 8 3 4 9 4
xyz = cat(3,x,y,z)
xyz =
xyz(:,:,1) = 8 4 8 2 8 4 xyz(:,:,2) = 10 5 5 6 5 8 xyz(:,:,3) = 9 8 3 4 9 4
result = median(xyz,3)
result = 2×3
9 5 5 4 8 4
##### 6 CommentsShow 4 older commentsHide 4 older comments
Jim McIntyre on 9 Jun 2023
Thank you. That answer works.
My original problem was in trying to calculate the median of the arrays without concatenating them. My original code was in the form:
myMedian = median(x(:),y(:),z(:))
James Tursa on 9 Jun 2023
The concatenation is going to take extra time and memory for the temporary data copies, which will slow performance. Could be avoided with a mex routine, but I wouldn't bother with that unless your variables are huge and the performance hit is significant.

Sign in to comment.

### Categories

Find more on Logical in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!