How to find the location of the first cell which contains a specific value in an array?

I have an array, all the cells are either with value 0 or value 1.
For each row, I need to find the location in form of (row, column) for the fist cell with value 1 in this row, and the location for the last cell with value 1 in this same row.
Then I will repeat this to all the other rows of the matrix.
I used double for loop, but I did succeed in storing the cell locations.
Can anyone help me?
Thank you.

 採用された回答

per isakson
per isakson 2014 年 9 月 9 日
編集済み: per isakson 2014 年 9 月 10 日
I'm not sure what you mean by "cell". Hint:
m = randi( [0,1], [12,12] );
n = nan( 2, 12 );
for jj = 1 : size( m, 2 )
n(:,jj) = [ find( m(:,jj)==1, 1, 'first' )
find( m(:,jj)==1, 1, 'last' ) ];
end
I'm not sure how it behaves if there is no value, 1, in a column.

6 件のコメント

zy
zy 2014 年 9 月 9 日
cell is just the single matrix, such as for a 10x10 matrix M, M(1,2) is a cell
Guillaume
Guillaume 2014 年 9 月 9 日
The proper name is element. Your usage of cell is confusing because matlab also has a data structure called cell array, whose syntax is slightly different to that of a matrix.
per isakson
per isakson 2014 年 9 月 9 日
Thus, the code behaves as you require(?). However, you didn't say what should be returned for columns, which don't include any 1.
Image Analyst
Image Analyst 2014 年 9 月 10 日
Guillaume's right. zy, please please read the FAQ on cells so that you don't confuse the terminology again: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F It makes a tremendous difference in how the code is built. Cell arrays are a lot more complicated than simple numerical arrays - you gotta pay for that extra flexibility. Avoid them if possible.
zy
zy 2014 年 9 月 10 日
Ok, I will read the FAQ for better understanding of those terminologies, thank you.
zy
zy 2014 年 9 月 11 日
Yes, per Isakson, it works, thank you very much. It helps me a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

zy
2014 年 9 月 9 日

コメント済み:

zy
2014 年 9 月 11 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by