MATLAB Answers

How to find streaks in a categorical vector?

1 ビュー (過去 30 日間)
buhmatlab
buhmatlab 2020 年 4 月 30 日
回答済み: Peter Perkins 2020 年 5 月 5 日
Hi,
is there a way to identify streaks in a categorical vector? Considering number vectors, I've found out that it might be useful to work with the functions find and diff but as this problem is concerned with a categorical vector, this does not work. Is there a workaround or would I need to convert my categorical vector into a number vector?
Many thanks for your help!
The table below illustrates my plan:
+--------+--------+
| CatVec | SerVec |
+--------+--------+
| A | 1 |
+--------+--------+
| C | 1 |
+--------+--------+
| A | 1 |
+--------+--------+
| B | 1 |
+--------+--------+
| B | 2 |
+--------+--------+
| C | 1 |
+--------+--------+
| A | 1 |
+--------+--------+
| A | 2 |
+--------+--------+
| A | 3 |
+--------+--------+
| B | 1 |
+--------+--------+
| C | 1 |
+--------+--------+
| C | 2 |
+--------+--------+

  2 件のコメント

Image Analyst
Image Analyst 2020 年 4 月 30 日
Did you look at the findgroups() function?
Ameer Hamza
Ameer Hamza 2020 年 4 月 30 日
To easily solve this problem, It will be much easier to use double datatype as compared to categorical. You can simply use double() to convert the categorical array to the double array.

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

採用された回答

Peter Perkins
Peter Perkins 2020 年 5 月 5 日
diff is a numeric function. categorical is not numeric. Ordinal categoricals have a mathematical ordering, but they don't have a "distance", so diff makes no sense for them.
But diff(x) is just x(2:end) - x(1:end-1). Again, categorical isn't numeric, but remember, the "trick" is not just diff(x), it's diff(x) > 0, so what you want is
d = c(2:end) ~= c(1:end-1)
At that point, you are exactly where you'd be if you had started with a numeric vector. Your runs (including singleton runs) begin at find(d) and the run lengths are diff(find(d)).

  0 件のコメント

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

その他の回答 (1 件)

Vimal Rathod
Vimal Rathod 2020 年 5 月 5 日
You can use "findgroups" function to make groups in Categorical Data. The function accepts the data as categorical data so you don't need to change your data.
Refer to the following link to know more about the function.

  0 件のコメント

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

Community Treasure Hunt

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

Start Hunting!

Translated by