New Reply

Fortran element by element array multiplication

 
Share Thread Thread Tools
Jan10-12, 08:39 AM   #1
 

Fortran element by element array multiplication


1. The problem statement, all variables and given/known data

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.



2. Relevant equations

For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



3. 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...
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Ants and carnivorous plants conspire for mutualistic feeding
>> Forecast for Titan: Wild weather could be ahead
>> Researchers stitch defects into the world's thinnest semiconductor
Jan10-12, 12:30 PM   #2
 
Mentor
Quote by Kwetla View Post
1. The problem statement, all variables and given/known data

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.



2. Relevant equations

For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



3. 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
Jan11-12, 03:56 AM   #3
 
Ah yes, that would work. Although i'm surprised there isn't a function for it.

Thanks a lot though!
Jan11-12, 12:16 PM   #4
 

Fortran element by element array multiplication


Vector times vector can also be a scalar, 32.
Jan11-12, 02:43 PM   #5
 
Recognitions:
Homework Helper Homework Help
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.
New Reply

Tags
arrays, fortran, matlab, multiplication
Thread Tools


Similar Threads for: Fortran element by element array multiplication
Thread Forum Replies
insert new element to array and how shift char type Engineering, Comp Sci, & Technology Homework 4
E and H pattern of 2 element antenna array. Electrical Engineering 1
Max Element in Array (CUDA C) Programming & Comp Sci 5
apply if/else if to each element in array in MATLAB Math & Science Software 5
locate maximum occuring element array in perl Programming & Comp Sci 6