[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[dennou-ruby:002392] Re: gpview, and max, min
- To: dennou-ruby@xxxxxxxxxxx
- Subject: [dennou-ruby:002392] Re: gpview, and max, min
- From: Masuo NAKANO <masuo@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 9 Aug 2005 00:43:15 +0900
竹広さま
なかの@現実逃避中です。:-P
> 
> > 僕はgpが2次元の時は
> > 
> >         idx = gp.val.eq(gp.max).where
> >         idx_x = idx % gp.coord(0).shape[0]
> >         idx_y = idx / gp.coord(0).shape[0]
> >         pos_x = gp.coord(0).val[idx_x]
> >         pos_y = gp.coord(1).val[idx_y]
> > 
> > こんな感じで最大値をとるところの位置情報を拾っています。
> > いかがでしょう?
> 
> へー, 勉強になりますねぇ. で, この手法は n 次元データに
> 拡張できるでしょうかね? 
whereメソッドは多次元配列を一次元配列とおもった時の添え字を返すみたいな
ので、ちょっと汚いですが、こんな感じで。。。
#いろいろ変数名がいまいちですが。
  idx = gp.val.eq(gp.max).where
  shape = gp.shape
  idx_array = Array.new
  (gp.shape.size - 1).times do |i|
    shape.pop
    num = 1
    shape.each do |s|
      num = num * s
    end
    idx_array.push( idx / num )
    idx = idx - num * idx_array[-1]
  end
  idx_array.push( idx % shape[0] )
  idx_array.reverse 
idx_arrayに各次元の添え字が入るはずです。
#15行もあるなぁ。もっと短くならないですかねぇ。
-- 
中野満寿男
九州大学大学院理学府地球惑星科学専攻
流体圏・宇宙圏科学講座 地球流体力学分野
masuo@xxxxxxxxxxxxxxxxxx
- References:
- [dennou-ruby:002386] gpview, and max, min
- [dennou-ruby:002387] Re: gpview, and max, min
- [dennou-ruby:002389] Re: gpview, and max, min
- [dennou-ruby:002390] Re: gpview, and max, min
- [dennou-ruby:002391] Re: gpview, and max, min