title: Fortran equivalent to matlab find - application to slicing matrix without memory duplication
body: <p>I use the command find quite a lot in matlab, and I wonder how to translate this smartly in fortran to extract a slice of an array. In matlab you can slice with either logicals or indexes, but in fortran you need indexes to slice. I'm aware of the intrinsic subroutines pack et al, but have never used them. Also, since I'm dealing with big matrices, I would like to avoid duplicating memory. I want the sliced matrix to be manipulated within a subroutine. I've read somewhere that slices of array were not duplicated. I don't know how this the slicing in done in matlab though. I'm puzzled also because in matlab some allocations are transparent to you. </p>
  
  <p>I'd like to know how to reproduce the examples below, and make sure I'm not duplicating stuff in memory and that it's actually elegant to do so. Otherwise, I would forget about slicing and just send the whole matrix(since it's by reference) and loop through an index array I...</p>
  
  <p>Matlab example 1: simply reproducing find</p>
  
  <pre><code>  v=[1 2 3 4];
    I=find(v==3);
  </code></pre>
  
  <p>Matlab example 2: </p>
  
  <pre><code>m=rand(4,4);
  bools=logical([ 1 0 0 1]);
  I=find(bools==1); 
  % which I could also do like: 
  I=1:size(m,1); 
  I=I(bools);
  
    for i=1:length(I)
        % here dealing with m(I(i)),:)  and do some computation
        % etc.
  </code></pre>
  
  <p>Example 3: just call a subroutine on m(I,:) , but using directly booleans for slicing</p>
  
  <pre><code>   foo( m(bools, :) , arg2, arg3 )
  </code></pre>
  
  <p>In advance thank you for your help!</p>