comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » I would like to average the first n columns based on duplicate values of the n+1th column
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: I would like to average the first n columns based on duplicate values of the n+1th column [message #93708 is a reply to message #93699] Tue, 04 October 2016 03:32 Go to previous messageGo to previous message
Markus Schmassmann is currently offline  Markus Schmassmann
Messages: 129
Registered: April 2016
Senior Member
On 10/03/2016 11:05 PM, belkaraza@web.de wrote:
> Can Someone help me solve this problem in IDL:
> "I have a matrix with duplicate numbers in one of the columns. I
> would
> like to average the rows with duplicate numbers. For example, I have
> duplicate values in a matrix A in column 3:
> A =
> 1 2 1
> 4 4 2
> 5 4 2
> 4 5 2
> 5 5 3
> 10 3 3
>
>
> B =
> 1 2 1
> 4.3333 4.3333 2.0000
> 7.5000 4.0000 3.0000
>
> where each row is the average values of the duplicate rows of column 3.
>
> Can anyone help?"
>
> found here:
> http://stackoverflow.com/questions/15270019/i-would-like-to- average-the-first-n-columns-based-on-duplicate-values-of-the -n1

if isa(A,/integer) then begin
h=histogram(A[2,*],reverse_indices=ri)
idx=where(h ne 0,n)
B=fltarr(3,n)
for i=0,n-1 do begin
if ri[idx[i]] eq ri[idx[i]+1]-1 then $
B[0,i]=A[*,ri[ri[idx[i]]:ri[idx[i]+1]-1]] else $
B[0,i]=mean(A[*,ri[ri[idx[i]]:ri[idx[i]+1]-1]],dim=2)
endfor
endif else
values=A[2,uniq(A[2,*],sort(A[2,*]))]
; if A[2,*] is already sorted, A[2,uniq(A[2,*])] is sufficient there
n=n_elements(values)
B=fltarr(3,n)
for i=0,n-1 do begin
w=where(A[2,*] eq values[i],cnt)
if w cnt 1 then B[0,i]=A[*,where(A[2,*] eq values[i])] else $
B[0,i]=mean(A[*,where(A[2,*] eq values[i])],dim=2,/nan)
endfor
endelse


hope that does it, Markus
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Writing a matrix
Next Topic: display GeoTIFF image automatically using cgGeoMap

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 01:36:38 PDT 2025

Total time taken to generate the page: 1.44207 seconds