How can I find the median of each element between 2 rows

Hello I am a bit new to MATLAB syntax and get confused
I have a matrix of size 3x10.
I want to find the median between each element in the first row, and each element in the second row.
How can I do this? I will need to end up with 10 median values

 Accepted Answer

% 3-by-10 matrix:
M = rand(3,10);
% median of each column:
median(M,1)
ans = 1×10
0.2939 0.2871 0.5671 0.8380 0.4499 0.5502 0.3408 0.9692 0.3886 0.3581
% 10-by-3 matrix:
M = rand(10,3);
% median of each row:
median(M,2)
ans = 10×1
0.8276 0.4754 0.6724 0.7976 0.4674 0.5106 0.6841 0.7998 0.5076 0.1331

4 Comments

Hi, sorry I will rephrase my question,
I learnt the median for example, from 1 to 10 is as:
ans = median(1:10)
My matrix (3x10) and I need to perform this operation between every corresponding element of the first row and the second row, so for example:
in position (1,1) of the matrix I have the value 100, and in position (2,1), I have 500, how can I do this operation for every corresponding element? (e.g. in this case the hard code would be median(100:500) )
Given integers a and b, with a<=b, the median of a:b (i.e., the median of all integers between a and b inclusive) is (a+b)/2, so to do that for each pair of elements in the first and second rows you could say:
M = randi(100,3,10) % 3x10 matrix of random integers between 1 and 100
M = 3×10
74 21 56 66 92 10 35 42 10 47 68 30 54 30 16 17 63 19 82 24 21 95 78 11 30 93 94 4 26 82
(M(1,:)+M(2,:))/2
ans = 1×10
71.0000 25.5000 55.0000 48.0000 54.0000 13.5000 49.0000 30.5000 46.0000 35.5000
If a>b, then what should median(a:b) give you? Maybe [] or maybe median(b:a)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 30 Apr 2022

Commented:

on 30 Apr 2022

Community Treasure Hunt

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

Start Hunting!