Using find function in columns and rows in matlab

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.

+4
source share
4 answers

Well, I can notice two errors:

  • You should use an array as the first argument to find . So, if you want to find the row number of the first element of each column , you should use find(Geometry(:, i), 1, 'first') .

  • find returns an empty array if the column contains only zeros. You should handle this case and decide which number you want to put in rownumber (for example, you can put -1 to indicate that the corresponding column does not contain non-zero elements).

Following the above, you can try the following:

 for i = 1:numcols tmp = find(Geometry(:, i), 1, 'first'); if(tmp) rowfirst(i) = tmp; else rowfirst(i) = -1; end; end; 
+1
source

0) bwmorph(Geometry,'close') dispense everything in one line. If the holes may be larger, try bwmorph(Geometry,'close',Inf) .

Regarding your attempt:

1) Instead of Geometry(i,1) it should be Geometry(i,:) .

2) Your real problem here is the empty matrices. Actually, what do you want rowfirst(i) be if there is no 1 in the i-th row?

+2
source

I'm sure there is a more efficient way to do this, but if you replace your call to find with this, it should work fine:

 find(Geometry(i,:), 1,'first') 

(otherwise you just look at the first cell of the i-th row. And == 1 is useless, since find already returns only nonzero elements, and your matrix is ​​binary)

+1
source

Use the AccumArray () function to find the min and max col (row) number.

Imagine that you find the last (first) row in each column containing NaN.

 a = [1 nan nan nan ; 2 2 3 4; 3 nan 3 3; 4 nan 4 4] 

This code gets the row indices for the last NaN in each column.

 [row,col] = find(isnan(a)) accumarray(col,row,[],@max) 

This code gets the row indices for the first NaN in each column.

 [row,col] = find(isnan(a)) accumarray(col,row,[],@min) 

Change the row and col variables to scan row by row rather than column by column.

This answer is inspired by finding the value and index of the minimum value in a matrix, grouped by column value

+1
source

Source: https://habr.com/ru/post/1302500/


All Articles