How to remove rest of the rows of a Column once it reaches a certain value

2 ビュー (過去 30 日間)
Avik Mahata
Avik Mahata 2021 年 6 月 28 日
コメント済み: Walter Roberson 2021 年 6 月 29 日
I have an autocorrelation function that needs to be integrated only up to where the time threshold first drops below Zero.
So I have a matrix such as A = [ 1 2 7 -2 0 2 9 1 2 0 9 -5].
I want to get A1 = [ 1 2 7 ].
How can I do this?

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 6 月 28 日
AZ = A(find(~A,1)-1)
However, find can be a bit heavy. Sometimes it can be faster to do
AZ = A(1:sum(cumprod(logical(A))))
This processes the entire vector multiple times, but sometimes it can be better than find. Especially if you have a 2D array that you want to do this kind of selection for on a per-row or per-column basis.
  2 件のコメント
Avik Mahata
Avik Mahata 2021 年 6 月 28 日
編集済み: Avik Mahata 2021 年 6 月 28 日
Thanks a lot for the reply. I just tried the second command and its giving me
AZ =[ 1 2 7 -2]
I wanted get AZ =[ 1 2 7]
Walter Roberson
Walter Roberson 2021 年 6 月 29 日
AZ = A(1:sum(cumprod(A>0)))

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by