title: Using find function on columns and rows in matlab
body: <p>I am having some problems with the find function in MATLAB. I have a matrix consisting of zeros and ones (representing the geometry of a structural element), where material is present when the matrix element = 1, and where no material is present when the matrix element = 0.  The matrix may have the general form shown below (it will update as the geometry is changed, but that isn't too important).</p>
  
  <pre><code>                 Geometry = [0 0 0 0 0 0 0 0 0 0;
                               0 0 1 0 1 0 1 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 0 0 0;
                               0 0 0 0 0 0 0 1 0 0;
                               0 0 0 0 0 0 0 0 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 1 1 1 0 1 0 0;
                               0 0 0 0 0 0 0 0 0 0;]
  </code></pre>
  
  <p>I'm trying to find the the rows and columns that are not continuously connected (i.e. where the row and columns are not all equal to 1 between the outer extents of the row or column) and then update them so they are all connected. I.e. the matrix above becomes:</p>
  
  <pre><code>                Geometry =  [0 0 0 0 0 0 0 0 0 0;
                               0 0 1 1 1 1 1 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 0 0 0 0 1 0 0;
                               0 0 1 1 1 1 1 1 0 0;
                               0 0 0 0 0 0 0 0 0 0;]
  </code></pre>
  
  <p>The problem I am having is I want to be able to find the indices of the first and last element that is equal to 1 in each row (and column), which will then be used to update the geoemtry matrix. </p>
  
  <p>Ideally, I want to represent these in vectors, so going across the columns, find the row number of the first element equal to 1 and store this in a vector called rowfirst. </p>
  
  <p>I.e.:</p>
  
  <pre><code>                    rowfirst = zeros(1,numcols)
                      for i = 1:numcols % Going across the columns
                      rowfirst(i) = find(Geometry(i,1) == 1, 1,'first') 
                                         % Store values in vector called rowfirst
                      end
  </code></pre>
  
  <p>and the repeat this for the columns and to find the last elements in each row. </p>
  
  <p>For some reason, I can't get the values to store properly in the vector, does anyone have an idea of where I'm going wrong?</p>
  
  <p>Thanks in advance. Please let me know if that isn't clear, as I may not have explained the problem very well.</p>