How can I count the number of times a specific value appears within a range in a 1D vector?

1 回表示 (過去 30 日間)
Given a 1D vector, x, that has 600000 elements, how can I count the number of times the value 1 appears after the 150th position (excluding 150)?

採用された回答

Daniel M
Daniel M 2020 年 4 月 27 日
sum(x(151:end) == 1)

その他の回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 27 日
編集済み: Ameer Hamza 2020 年 4 月 27 日
Try this.
x = randi([1 10], 1, 600000); % random vector
result = sum(x(151:end)==1);

per isakson
per isakson 2020 年 4 月 27 日
If x is a vector of whole numbers
sum( double( x(151:end)==1 ) )
else
sum( double( abs(x(151:end)-1) < small_number ) )
  2 件のコメント
Caesar Hernandez
Caesar Hernandez 2020 年 4 月 29 日
The top line also worked. The bottom line did not.
per isakson
per isakson 2020 年 4 月 29 日
It works here. Did it return any error message?

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by