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

2 ビュー (過去 30 日間)
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

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 16 日
B = accumarray(A(:,1),A(:,2),[],@median);
out = [A, B(A(:,1))];
  2 件のコメント
Emerson De Souza
Emerson De Souza 2012 年 9 月 16 日
Hi Andrei, I have a problem:
The values of the first column may be 1.5 instead of 1 and so on for the others rows. I this case I obtain the following error:
Error using accumarray First input SUBS must contain positive integer subscripts.
Do you know what to change in the command to make it more general?
Thank you
Emerson
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 17 日
yes,
[c,c,c] = unique(A(:,1));
B = accumarray(c,A(:,2),[],@median);
out = [A, B(A(:,1))];

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 16 日
編集済み: Azzi Abdelmalek 2012 年 9 月 16 日
B=squeeze(median(reshape(A(:,2),3,1,size(A,1)/3)))
%A must contains a multiple of 3 rows, if not, we have to complete with nan or zero values, write at the begening this code
nc=mod(size(A,1),3);
if nc>0;
A=[A;nan(3-nc,2)]
end
  5 件のコメント
Emerson De Souza
Emerson De Souza 2012 年 9 月 16 日
Thank you Azzi,
your suggestion works now. I only don't understand what idx1 is doing.
I also get a red line for B(k)=..... with the comment:
The variable B appears to change size on every iteration (within a script). Consider preallocating for speed. I don't understand what that means.
Thank you again for your help
Emerson
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 17 日
編集済み: Azzi Abdelmalek 2012 年 9 月 17 日
for our example
idx1 =
1 3 1 is repeating from index 1 to 3
4 7 2 is repeating from index 4 to 7
8 12 3 is repeating from index 8 to 12
13 14 4 is repeating from index 13 to 14
B is changing a size because it's in the loop, B(1), B(2),... then B(k)
to make preallocation,( in case we work with big array)
B=zeros(1,numel(idx))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by