How can I extract the first non-zero in several specific segments of the same matrix?

1 回表示 (過去 30 日間)
I'd like to find the first non-zero in several segments of the same large matrix.
For example: x = [3 2 3 4 5 6 6 8 6 10; 0 0 1 0 2 0 0 1 0 1]';
Now within x(1:3, :) and x(7:10, :) I'd like to extract the first non-zero in the second column and write out the corresponding value of the first column...no idea how...any help much appreciated. Patricia

採用された回答

Matt Kindig
Matt Kindig 2013 年 7 月 5 日
ix1 = find(x(1:3,2),1,'first'); %index of first non-zero in x(1:3,2);
x1 = x(ix1,1); %corresponding first-column value
ix2 = find(x(7:10,2),1,'first'); %index of first non-zero in x(7:10,2);
x2 = x(ix2,1); %corresponding first-column value
  1 件のコメント
Phillip
Phillip 2013 年 7 月 5 日
Thanks Matt for your reply - x2 not quite right (am after x2 = 8) but got it now...thanks for your time!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by