フィルターのクリア

Is there a way to count the integers in a matrix that fall within a certain range?

5 ビュー (過去 30 日間)
Eileen
Eileen 2022 年 2 月 9 日
コメント済み: Chunru 2022 年 2 月 9 日
I am working on a problem that gives me a 3x10 matrix with numbers ranging from 1 to 100. I want to count the number of integers that fall within a ceratin catagory such as 0 to 19, 20 to 39, 40 to 59, etc. Is there a way for me to find the sum of integers within these specific ranges? I'm still very new to MatLab. Thank you.
  1 件のコメント
Voss
Voss 2022 年 2 月 9 日
編集済み: Voss 2022 年 2 月 9 日
Questions: Might your matrix have some non-integer elements? Or is it always all integers? And if it does have non-integers, is it correct that you don't want to include those non-integers in the sums?

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

回答 (2 件)

Chunru
Chunru 2022 年 2 月 9 日
a = randi(100, [3 10]);
idx = a>=20 & a<=29;
x = sum(a(idx))
x = 20
  2 件のコメント
Eileen
Eileen 2022 年 2 月 9 日
using x = sum(a(idx)) gave me some pretty large numbers for some reason so I ended up just using sum(sum(idx))
Chunru
Chunru 2022 年 2 月 9 日
You are right. sum(idx(:)) will do the counting while the code above sum all the numbers in the range.

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


Matt J
Matt J 2022 年 2 月 9 日
Yes, you can use histcounts.
counts=histcounts(yourMatrix(:),[0,20,40,60])

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by