Using find function on 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 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).

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'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:

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 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.

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.

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 the repeat this for the columns and to find the last elements in each row.

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?

Thanks in advance. Please let me know if that isn't clear, as I may not have explained the problem very well.

-------------Problems Reply------------

Ok, I can spot two mistakes:

  • You should use an array as the first argument of find. So, if you want to find the row number of the first element of each column, then 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 what number you want to put into rownumber (e.g. you can put -1, to indicate that the corresponding column contains no non-zero elements).

Following the above, you can try this:

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

0) bwmorph(Geometry,'close') dose it all in one line. If the holes may be bigger, try bwmorph(Geometry,'close',Inf).

Regarding your attempt:

1) It should be Geometry(i,:) instead of Geometry(i,1).

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

I'm pretty sure there's a more efficient way of doing this, but if you replace your call to find with this, it should work ok:

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

(otherwise you're just looking at the first cell of the ith row. And the == 1 is useless, since find already returns only non-zero elements, and your matrix is binary)

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

Imagine finding the last (first) row in each column that contains a 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)

Swap the row and col variables to scan row-wise instead of column-wise.

This answer inspired by Matlab: Finding value and index of min value in a matrix, grouped by column values

Category:matlab Views:0 Time:2010-02-26

Related post

  • Min functions for columns and rows 2012-10-05

    I need to find a min function that acts liek DMIN but uses row titles instead of in columns I have a series of columns by month for 15 years and a range that has max min averages by those months and I want to find an overall minimum (and also a max)

  • How to make Column to Row without an aggregate function in sql server 2005/8? 2010-10-26

    For example, I need to change from to . I know PIVOT is for that, but it requires an aggregate function; and for my case, I donot need to aggregate only need column to row. You can use the following sample data: CREATE TABLE[StudentScores] ( [UserNam

  • Self-reference for cell, column and row in worksheet functions 2011-06-13

    In a worksheet function in Excel, how do you self-reference the cell, column or row you're in? Note that this is extremely useful for conditional formatting. --------------Solutions------------- where F13 is the cell you need to reference: =CELL("Row

  • VBA: Complex VLOOKUP function (two-column match vertically, one-row match horizontally). Advice on method 2012-03-09

    I'm looking for advice on how to approach a certain problem. In short, I have a matrix with two descriptive columns to the left (Y-axis) and a range of durations/time periods on the X-axis. The first column include bond types, and the second currency

  • How can I use a SUMIF type function when numbers are not a continuous column or row? (e.g. sum any of the following that are non-zero: X2,X5,X11,X15) 2014-07-24

    How can I use a SUMIF type function when numbers are not a continuous column or row? e.g. I want to sum any of the following cells that do not contain the error "#DIV/0!" X2,X5,X11,X15. --------------Solutions------------- Couldn't you use something

  • How can I get and put a value into a certain column and row? 2009-09-24

    I am having difficulties doing this program. It's transportation method using intuitive approach. you will first create two inputs that will describe how many rows and columns will the user wish to have. and then, after clicking a submit button, a de

  • How to get linux console $COLUMNS and $ROWS from PHP cli? 2010-02-04

    I'm currently creating a new neat CLI library for PHP, and i'd like to figure out the width/height of the console it's running in. I've tried many things like digging through $_ENV, exec("echo $COLUMNS"), etc, but no result, while if i type echo $COL

  • Table: each cell links to url containing column and row data 2010-07-08

    I want each cell in my table (excluding headers and has to be a number) to link to a URL which is formed using the position of the cell, ie the cell number and column number of it. Eg a cell will link to example.com/hello.html?column=1&row=3 Idea

  • How to read columns and rows with C#? 2010-11-20

    Hello I'm in need of a code to read columns and rows for C#. I've come this far: obj.MysqlQUERY("SELECT * FROM `players` WHERE name = "+name+";"); // my query function Grateful for help;] --------------Solutions------------- Here is a standard block

  • Swapping either Columns or Rows in a Ruby Matrix 2011-01-30

    I want to swap either rows or columns in an array of arrays (i.e. a matrix). I have found this swap method online, and I extended it with my mutate function, where the board.matrix is the array of arrays: # Swap to elements of an Array def swap!(a,b)

  • C# matrix keyed by column and row string values 2011-06-13

    I want to create a c# data structure that will have a variable number of columns, and a variable number of rows. I will insert values into the 'cells'. I want to be able to index both the columns and the rows using strings, not integers, so that I ca

  • changing rows into column and column into rows of that 2d array 2011-07-06

    I want to change rows into column and column into rows of that 2-D array I want a program which takes input and gives output as below. Input: 1 2 3 4 5 6 Output: 1 4 2 5 3 6 Input: 1 2 3 4 5 6 7 8 9 Output: 1 4 7 2 5 8 3 6 9 I did a sample which in h

  • In MS SQL how to split a column into rows with no delimiter 2011-07-08

    I have data in a table which looks like this (worth noting its not CSV seperated) It needs to be split in to single chars Data abcde want to convert it to this Data a b d c e I have looked on the internet but have not found the answer --------------S

  • LINQ swap columns into rows 2011-08-05

    Is there a fancy LINQ expression that could allow me to do the following in a much more simpler fashion. I have a List<List<double>>, assuming the List are columns in a 2d matrix, I want to swap the list of columns into a list of rows. I

  • Why join query containing a sum function returns just 1 row 2011-10-11

    I have 2 tables. Problem and problem_votes. There are many rows in the problems table and just one in the votes table. I have this query: select problems.problem_id, creator_member_id, problem_title, problem_description, sum( vote ) as totalVotes, pr

  • Determine column or row count with list elements as inline-block + a division quirk 2011-12-22

    First question. Hope this isn't a repeat. I have an unordered list of thumbnails displayed as inline-blocks. The images all line up in a nice, clean grid. But, depending on the size of their wrapper, the number of columns and rows can vary. I have da

  • Count unique cells in a column or row of an HTML table with javascript/jquery 2012-01-09

    Is there a simple way to count the unique cells in a column (or row) in an HTML table with jQuery or plain javascript? Example: (table with only one column) melon banana melon strawberry The counter would be 3. Thanks for your help! --------------Sol

  • how to dynamically change html table single column 60 rows to three column 20 rows 2012-02-23

    I am trying to seperate a dynamically loaded 60 rows single column html table to three column 20 rows using jquery in jsp. example i Have a loaded table like this test 1 test 2 test 3 test 4 test 5 test 6 test 7 test 8 test 9 test 10 i want to conver

  • Function to count mysql rows using WHERE 2012-02-24

    am making function to count rows using "WHERE", but i get a mysql error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\test\test\index.php on line 9 Unknown column '1' in 'where clause' here is my

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.093 (s). 11 q(s)