How to find the location of the first cell which contains a specific value in an array?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!