- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
could someone please enlighten an old amateur F77-hacker as to the syntax for array operations in F90? I want to multiply, divide, add and subtract segments of arrays, of equal shape, as well as scalars, in an element-by-element fashion. I thought maybe this could be written without loops, in "Matlab-style". E.g., is the following OK?
real a(20),b(30,2),c,d(20)
.
.
.
a(1:10)=b(11:20,1)*c/d(1:10)
What I want is a(1)=b(11,1)*c/d(1), a(2)=b(12,1)*c/d(2), etc.
Thanks,
Olof
could someone please enlighten an old amateur F77-hacker as to the syntax for array operations in F90? I want to multiply, divide, add and subtract segments of arrays, of equal shape, as well as scalars, in an element-by-element fashion. I thought maybe this could be written without loops, in "Matlab-style". E.g., is the following OK?
real a(20),b(30,2),c,d(20)
.
.
.
a(1:10)=b(11:20,1)*c/d(1:10)
What I want is a(1)=b(11,1)*c/d(1), a(2)=b(12,1)*c/d(2), etc.
Thanks,
Olof
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This should work just fine. You can also invoke elemental functions on arrays, like sin(a(1:10)). In Fortran, if you need to use a nondefault stride in an array section, it comes last in the triplet: Matlab's a(10:-1:1) would translate to a(10:1:-1) in Fortran. Also be aware that if you have two arrays such as real e(3,3), f(3,3) then e*f denotes the elemental, rather than the matrix product of the arrays; if you want the matrix product, use matmul(e,f). Fortran has no transformational intrinsic for matrix division.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the answer, James :)!
On the same topic, is the following OK?
real a(3),b,c(3)
b=2
c=(/1,2,3/)
a=max(b,c)
What I'm looking for is that a(1)=max(b,c(1)), a(2)=max(b,c(2)) and a(3)=max(b,c(3)). As I interpret the documentation this is not correct. Does max take array arguments at all?
Thanks,
Olof
On the same topic, is the following OK?
real a(3),b,c(3)
b=2
c=(/1,2,3/)
a=max(b,c)
What I'm looking for is that a(1)=max(b,c(1)), a(2)=max(b,c(2)) and a(3)=max(b,c(3)). As I interpret the documentation this is not correct. Does max take array arguments at all?
Thanks,
Olof
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Are you sure that you have the most recent version of the documentation? Under Help|Index, typing in max and choosing the upper-case MAX keyword I get the documentation for the function MAX. The first word of the the document (after MAX) indicates that the intrinsic is elemental. If we now look for the word elemental in the index we see a likely entry entitled "Elemental intrinsic procedures" which you should read. Strange that they used the transformational intrinsic s = sum(a) as an example under the "Elemental procedures" bullet, though. Also there is a subtopic "references to" in the index panel that in fact shows an example of the elemental invocation of MAX! MAX works just as you hoped it should and the documentation says so, you just have to read it more thoroughly.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page