1

I want to check all rows of an array for a logical condition. I used function ALL as described in GNU GCC guide https://gcc.gnu.org/onlinedocs/gfortran/ALL.html

Here is a sample code:

program test3
implicit none
real, allocatable, dimension (:,:) :: mat1
integer :: i,j,k,r
logical :: lg
r=3
allocate(mat1(r,r))
mat1=transpose( reshape( (/-1,-2,-3,-4,-5,-6,-7,-8,-9/), (/3,3/)))
lg=all (abs(mat1)<10,1)
write (*,*) lg
end program

In this program, I want to check whether absolute value of all elements along all rows is less than 10. But I am getting error

lg=all (abs(mat1)<10,1)
Error: Incompatible ranks 0 and 1 in assignment

Any idea about this error or how to do this check?

1
  • You want, for each row that logical test? That is, you want one logical result for each row of mat1? Three, rather than one, results? Jun 14, 2015 at 20:41

2 Answers 2

2
Error: Incompatible ranks 0 and 1 in assignment

means that you are trying to assign a rank-1 array to a scalar variable.

In this case lg is your scalar left-hand side. As you want to test the condition against each row (as supported by using the [dim=]1 specifier) it makes sense for lg to be an array of rank-1 with as many elements as there are rows in mat1.

That said, because Fortran uses column-major storage using ALL(...,dim=1) here is actually giving you the test result along columns. In general, the result of ALL(L, dim=n) is of shape [d_1, d_2, ..., d_{n-1}, d_{n+1}, ..., d_m] where the shape of L is [d_1, ..., d_m].

[As noted in another answer the result of ALL(L) is a scalar. If this is what you want here, then I may have something to say about potential confusion with the language of the formal description of ALL.]

2

Use this to get scalar logical:

lg = all(abs(mat1) < 10)
2
  • Does this answer give a complete solution to the question? If not, it should probably be a comment. Jun 14, 2015 at 22:47
  • @NickVolynkin This is a complete answer. The question contains a link to the documentation, so no additional explanation is necessary. The documentation is clear enough: "ALL(MASK) returns a scalar value of type LOGICAL... If DIM is present, then ALL(MASK, DIM) returns an array". Since the question is about testing all the elements, the scalar form is a solution.
    – Andrey
    Jun 15, 2015 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.