文字が何種類あるかカウントしたい

34 views (last 30 days)
丈太郎 森川
丈太郎 森川 on 16 Sep 2021
Commented: 丈太郎 森川 on 16 Sep 2021
例えば文字列で['A1';'A1';'A1';'A2';'A2';'B1';'B1';'B2';'B3']であれば、答えは文字がA1,A2,B1,B2,B3の5種類なので5になるコードを教えていただけないでしょうか。文字内容はランダムになりますが、同じ文字は並びます。
ロット管理のイメージです。データは何ロットあるか?
よろしくお願い致します。

Accepted Answer

Kojiro Saito
Kojiro Saito on 16 Sep 2021
関数uniqueを使って重複を取り除いてから、lengthで個数を求める方法でもできますし、
length(unique(['A1';'A1';'A1';'A2';'A2';'B1';'B1';'B2';'B3'], 'rows'))
ans = 5
カテゴリー配列にした後にcategoriesで重複を取り除いてからlengthで個数を求める方法でもできます。
length(categories(categorical(cellstr(['A1';'A1';'A1';'A2';'A2';'B1';'B1';'B2';'B3']))))
ans = 5
  1 Comment
丈太郎 森川
丈太郎 森川 on 16 Sep 2021
早速の回答ありがとうございます。大変助かります。

Sign in to comment.

More Answers (0)

Categories

Find more on Matrix Indexing in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!