フィルターのクリア

How can I count the occurrences of each element in a matrix in MATLAB?

12 ビュー (過去 30 日間)
Tracy Campbell
Tracy Campbell 2017 年 5 月 25 日
回答済み: Geoff Hayes 2017 年 5 月 25 日
I'm trying to figure out how to count how often certain numbers appear in a specific column of my matrix (each number is coded for a different category). Very new to MATLAB, any help is appreciated!

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 5 月 25 日
Tracy - one way is to use ismemeber (though this can be "slow"). For example, suppose your column array is constructed as
c = randi(15, 25, 1); % so generate 25 random numbers from 1 to 15
m = [10 15]; % m is the set of integers that you trying to match on
sum(ismember(c,m))
Since ismember(c,m) returns a logical array of ones (indicating a match in m) and zeros (indicating no match) then we can sum up all the ones to determine how many elements of c are in m.
Note that the above is only valid for integers.

その他の回答 (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