Prev: Amplitude and phase correction by a known transfer function
Next: adding impulsive noise to yuv sequence
From: enviro on 31 May 2010 07:24 How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea? input=[ 2 10 50 1 20 100 2 30 150 1 40 200 1 60 300]; output=[ 1 40 100 2 20 200 ];
From: us on 31 May 2010 11:40 enviro <farhadnejadkoorki(a)yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root(a)gallium.mathforum.org>... > How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea? > > input=[ > 2 10 50 > 1 20 100 > 2 30 150 > 1 40 200 > 1 60 300]; > > output=[ > 1 40 100 > 2 20 200 > ]; a hint: - look at jd's FEX submission http://www.mathworks.com/matlabcentral/fileexchange/8354-consolidator us
From: Roger Stafford on 31 May 2010 17:13 enviro <farhadnejadkoorki(a)yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root(a)gallium.mathforum.org>... > How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea? > > input=[ > 2 10 50 > 1 20 100 > 2 30 150 > 1 40 200 > 1 60 300]; > > output=[ > 1 40 100 > 2 20 200 > ]; - - - - - - - - - Call your input matrix A and the desired output B. Then do this: B = sortrows(A,1); % Sort according to col. 1 p = find([true;diff(B(:,1))~=0;true]); % Point to steps in col. 1 n = size(A,2); C = cumsum([zeros(1,n-1);B(:,2:n)]); % cumsums in other cols. B = [B(p(1:end-1),1),diff(C(p,:))./repmat(diff(p),1,n-1)]; % Get means Comments: In the last line, diff(C(p,:)) gets the needed sums by taking differences of the appropriate cumulative sum values. Dividing by diff(p) counts gives the corresponding mean values. Note: Presumably you meant the column 3 values of your output to be the reversal of what you wrote. Roger Stafford
From: Steve Amphlett on 31 May 2010 18:59
enviro <farhadnejadkoorki(a)yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root(a)gallium.mathforum.org>... > How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea? > > input=[ > 2 10 50 > 1 20 100 > 2 30 150 > 1 40 200 > 1 60 300]; > > output=[ > 1 40 100 > 2 20 200 > ]; Does column 1 always contain positive integers? |