I am having some problems with the find function in MATLAB. I have a matrix consisting of zeros and ones (representing the geometry of the structural element), where the material is present when the matrix element = 1, and where there is no material when the matrix element = 0. The matrix can have a general view (it will update as it changes geometry, but itβs not too important).
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;]
I try to find rows and columns that are not connected continuously (i.e. where the row and columns are not all equal to 1 between the external extents of the row or column), and then update them so that they are all connected. That is, the matrix above becomes:
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;]
The problem I am facing is that I want to be able to find the indices of the first and last elements equal to 1 in each row (and column), which will then be used to update the geometry matrix.
Ideally, I want to represent them in vectors, therefore, going through the columns, find the line number of the first element equal to 1, and save it in a vector called rowfirst.
i.e:.
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
and repeat this for the columns and find the last items in each row.
For some reason, I cannot store the values ββin the vector correctly, does anyone have an idea where I am going wrong?
Thanks in advance. Please let me know if this is not clear, as I may not have explained the problem very well.