Posts

Showing posts with the label logical-operators

Fortran - Logical Indexing

Fortran - Logical Indexing Suppose I have a matrix A which is (m x n) and a vector B which is (m x 1) . This vector B is a vector of zeros and ones. A (m x n) B (m x 1) B Also let the scalar s be the sum of the elements in B . s B I want to create a matrix C which is s x n corresponding to the rows of B which equal 1, and a vector D which is s x 1, with the position of those elements in A . C s x n B D A Take as an example: A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12; 13, 14, 15 ] B = [0; 1; 0; 1; 1] Then: C = [ 4, 5, 6; 10, 11, 12; 13, 14, 15 ] and D = [2; 4 5] As of now my fortran code looks like: PROGRAM test1 IMPLICIT NONE REAL, DIMENSION(5,3) :: A INTEGER, DIMENSION(5,1) :: B = 0 INTEGER :: i, j, k k = 1 !Create A matrix do i=1,5 do j=1,3 A(i,j) = k k = k+1 end do end do !Create B matrix B(2,1) = 1 B(4,1) = 1 B(5,1) = 1 end program In matlab I could create C simply b...