Fortran element by element array multiplication

In summary, the person is having trouble multiplying two vectors together in Fortran. They want to multiply each element in one vector by its corresponding element in the other vector, similar to how it is done in Matlab using A.*B. They have tried using dot product methods but have not been successful. One potential solution is to use a loop and multiply each element individually. Another solution is to use the Fortran 90 syntax of C = A*B or C(1:N) = A(1:N)*B(1:N), assuming all variables and arrays are declared and initialized.
  • #1
Kwetla
3
0

Homework Statement



Hello,

I'm having a problem in multiplying two vectors together in a specific way in Fortran. I can do it in Matlab, but can't work out how to do it in Fortran.

The problem is that i want to multiply two vectors together, but only each element by it's corresponding element in the other vector. In Matlab you simply use A.*B which works perfectly.



Homework Equations



For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



The Attempt at a Solution



Any attempt i make in Fortran tries to multiply all the elements together (usually by dot product etc), but i just want the first element multiplied by the first element, and then the second mulitplied by the second etc.

Sorry about the strange formatting, i tried to crowbar this question into the template...
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Kwetla said:

Homework Statement



Hello,

I'm having a problem in multiplying two vectors together in a specific way in Fortran. I can do it in Matlab, but can't work out how to do it in Fortran.

The problem is that i want to multiply two vectors together, but only each element by it's corresponding element in the other vector. In Matlab you simply use A.*B which works perfectly.



Homework Equations



For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



The Attempt at a Solution



Any attempt i make in Fortran tries to multiply all the elements together (usually by dot product etc), but i just want the first element multiplied by the first element, and then the second mulitplied by the second etc.

Sorry about the strange formatting, i tried to crowbar this question into the template...
Assuming that all variables and the three arrays are declared and that A and B are initialized, this should work.
Code:
N = 3
DO I = 1, N
  C(I) = A(I) * B(I)
END DO
 
  • #3
Ah yes, that would work. Although I'm surprised there isn't a function for it.

Thanks a lot though!
 
  • #4
Vector times vector can also be a scalar, 32.
 
  • #5
If you are using at least Fortran 90, I believe the following will work:

C = A*B

or

C(1:N) = A(1:N)*B(1:N)

The 1st version assumes A,B,C have the same dimension. The 2nd version is safer.
 

What is element by element array multiplication in Fortran?

Element by element array multiplication in Fortran refers to the process of multiplying each element in one array with the corresponding element in another array, resulting in a new array with the same dimensions as the original arrays.

How is element by element array multiplication different from regular array multiplication in Fortran?

Regular array multiplication in Fortran involves multiplying two arrays of the same dimensions, resulting in a single array with the same dimensions. Element by element array multiplication, on the other hand, multiplies each element in one array with the corresponding element in another array, resulting in a new array with the same dimensions as the original arrays.

What are the benefits of using element by element array multiplication in Fortran?

Element by element array multiplication in Fortran allows for efficient and concise coding, as it eliminates the need for nested loops that are typically required for regular array multiplication. This can also lead to faster execution times and improved performance.

How do you perform element by element array multiplication in Fortran?

To perform element by element array multiplication in Fortran, you can use the built-in "matmul" function, which takes in two arrays and multiplies them element by element. Alternatively, you can also use a "do" loop to iterate through each element and perform the multiplication manually.

In what scenarios is element by element array multiplication commonly used in Fortran?

Element by element array multiplication in Fortran is commonly used in scientific computing and data analysis, where large arrays of data need to be multiplied efficiently. It is also often used in image processing, mathematical modeling, and other applications that involve working with multidimensional arrays.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
873
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Mechanical Engineering
Replies
2
Views
806
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top