F90: adding vector to 2D array

In summary, the conversation discusses a question about adding a vector to a 2d dimensional array in Fortran 90. The speaker finds using do loops for this task to be cumbersome and asks if there is a simpler way to add values to the first value of the array. Suggestions are given to either create a function or define a custom operator to achieve this.
  • #1
dwnlder
1
0
hi. i have a quick question regarding fortran 90. i often need to add a vector to a 2d dimensional array, and i find it a bit to 'bulky' to always use do loops.

let's say i have this:
Code:
do i=1, 20
   a(:, i)=a(:, i)+b(:)
end do

is there a simple(r) way to tell fortran that i want to add the values to the 'first' value of the array? so i could use something as simple as
Code:
a(:, :)=a(:, :)+b(:)

thx in advance for the answers and suggestions.
 
Technology news on Phys.org
  • #2
You could either write a quick FUNCTION or perhaps define a custom operator.
 

Related to F90: adding vector to 2D array

1. How do I add a vector to a 2D array in F90?

To add a vector to a 2D array in F90, you can use the built-in array addition operator (+) or the intrinsic function SUM. For example, if your vector is called "vec" and your 2D array is called "arr", you can use "arr = arr + vec" or "arr = SUM(arr,vec)" to add the vector to the array.

2. Can I add a vector of different dimensions to a 2D array in F90?

No, the dimensions of the vector and the array must match in order to be added together. If they do not match, you can use the RESHAPE function to reshape the vector to the same dimensions as the array before adding them together.

3. How do I specify which axis to add the vector to in a 2D array?

In F90, the default behavior is to add the vector to the first dimension of the array. To specify a different axis, you can use the TRANSPOSE function to swap the dimensions of the array, add the vector, and then transpose the array back to its original dimensions.

4. Is there a more efficient way to add a vector to a 2D array in F90?

Yes, if you are adding the same vector to multiple rows or columns of a 2D array, you can use the built-in broadcasting feature in F90 to add the vector to all rows or columns at once. This can be done by using the RESHAPE function to create a 3D array with the vector as one of its dimensions, and then using the array addition operator (+) to add it to the 2D array.

5. Can I add a vector to only certain elements of a 2D array in F90?

Yes, you can use conditional statements and the WHERE function to specify which elements of the array you want to add the vector to. This allows for more control over the addition process and can be useful in certain situations.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top