Broadcast array multiplication in Fortran 90/95

后端 未结 3 494
花落未央
花落未央 2020-12-19 07:27

I was wondering that would there be a better (succinct) way to code this in Fortran? I am trying to multiply each column of a(3, 3) by each value in b(3)<

相关标签:
3条回答
  • 2020-12-19 07:33

    If you are reusing a particular b frequently you could define:

     b(3, 3)=reshape([1, 1, 1, 2, 2, 2, 3, 3, 3], [3, 3])
    

    then you just can do:

     a=a*b
    

    ..

    0 讨论(0)
  • 2020-12-19 07:39

    The do loop can be replaced by a one-liner using FORALL:

    forall (i=1:3) a(:, i) = a(:, i) * b(i)
    
    0 讨论(0)
  • 2020-12-19 07:55

    The expression

    a * SPREAD(b,1,3)
    

    will produce the same result as your loop. I'll leave it to you and to others to judge whether this is more succinct or in any way better than the loop.

    0 讨论(0)
提交回复
热议问题