MATLAB: How to calculate the median of a column depending on the value of another column

arrayMATLABmatrix manipulationstatistics

INTRODUCTION: I have two columns of values. The values of the first column are partially constant and the values of the second column are arbitrary ones.
GOAL: I want to build a third column with values of median for each group of constant value of the first column.
EXAMPLE:
A=[1 3;
1 2;
1 3;
2 4;
2 4;
2 3;
2 4;
3 5;
3 1;
3 1;
3 1;
3 2;
4 3;
4 2];
B1=median(A(1:3,2));
B2=median(A(4:7, 2));
B3=median(A(8:12, 2));
B4=median(A(13:14, 2));
B=[B1 B2 B3 B4]';
PROBLEM: The number m of rows are typically much larger than only 14 and makes impossible to write the commands B1 until BN per hand.
I wonder if someone could tell me how to write some command lines that makes this automatically.
Thank you in advance for your help
Emerson

Best Answer

B = accumarray(A(:,1),A(:,2),[],@median);
out = [A, B(A(:,1))];