Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Fortran array contains value?

I've seen this asked for other languages, but having just found out how nicely Fortran can handle arrays, I thought there might be an easy way to do this without loops.

Currently I'm searching over a 3D array looking at 'nearest neighbours' to see if they contain the letter 'n', and whenever it finds this value, I want it to perform some clusterLabel assignment (which isn't relevant for this question)

I wanted to use if(lastNeighArray.eq."n") then...<rest of code> but for obvious reasons it doesn't like checking an array against a value. Neither does it like me using lastNeighArray(:), even though I'd like it to check each of the elements one at a time. where(lastNeighArray.eq."n") doesn't work as I have a case statement inside the where loop and I get the error WHERE statements and constructs must not be nested.

So I'm a little stuck. What I really want is something like when(lastNeighArray.eq."n") but that doesn't exist.

I've also looked at any and forall but they don't seem like the right choice.

like image 243
AncientSwordRage Avatar asked Dec 01 '11 11:12

AncientSwordRage


People also ask

Is Fortran zero indexed?

By default, Fortran indexes the first element of an array as 1. C and C++ index it as 0.

How do I count in Fortran?

elements in a logical MASK , or, if the DIM argument is supplied, counts the number of elements along each row of the array in the DIM direction. If the array has zero size, or all of the elements of MASK are . FALSE. , then the result is 0 . The type shall be LOGICAL .


1 Answers

ANY should actually be the right choice

if ( ANY( lastNeighArray=="n" ) ) then 

there is also ALL if you wanted the whole array to contain that value.

like image 199
Vladimir F Героям слава Avatar answered Sep 27 '22 20:09

Vladimir F Героям слава