How to identify a specific location within a matrix based on a value

6 ビュー (過去 30 日間)
Tricia
Tricia 2014 年 9 月 8 日
コメント済み: yonatan gerufi 2014 年 9 月 8 日
Hi,
I have a matrix of all 0s and 1s. I'd like to identify the exact location of the first nonzero value of each row. I've found several different ways to identify the nonzero value (e.g., find()) but I don't need to know the value (I know it's 1), I need to know it's precise location.
I'm working on a program that will identify the first nonzero value in each row and compare those locations to another matrix and eventually I would like to shift matrix 2 to match the first nonzero values in matrix 1.
Any help with identifying the location of a specific value in a row (in this case the first nonzero value) is greatly appreciated.
Thanks.

採用された回答

yonatan gerufi
yonatan gerufi 2014 年 9 月 8 日
編集済み: yonatan gerufi 2014 年 9 月 8 日
possible solution:
% construct random binary matrix
a=round(rand(5,10));
% find first minimal value:
[value,position] = max(a,[],2)
now 'position' will contain the position of first maximum element in a row.
'value' will contain this element value.
unless all the line is 0's (you should check it at 'value') 'position' will give you the first one!
if you have any more ways please post them.
good luck!
  3 件のコメント
Tricia
Tricia 2014 年 9 月 8 日
I do have one follow up question actually. I'm having trouble returning the 'value' results into a new array. I'd like to save the column positions as a completely new array, if that's possible. Do you know if it is?
Thanks again!
yonatan gerufi
yonatan gerufi 2014 年 9 月 8 日
sorry, i don't i understand your question.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by