How to deal with repeated data in a column of a table

1 回表示 (過去 30 日間)
QIAO WANG
QIAO WANG 2019 年 10 月 30 日
コメント済み: QIAO WANG 2019 年 11 月 13 日
Hey,
I'm currently trying to process a table. What I want is to extract a sequence from a column of it. Before I used unique but apparently the result wasn't what I intended for.
An example of the table could be like below. I want to extract [22 24 22] for vid 59. However, unique only gave me [22 24], which is not what I want. So, I hope I've explained my question well. Appreciate your advice on this!!
TableC.PNG

採用された回答

Cyrus Tirband
Cyrus Tirband 2019 年 10 月 30 日
編集済み: Cyrus Tirband 2019 年 10 月 30 日
It sounds like you're looking to remove repeating elements that are adjecent. If that is the case, you can test your array for differences between each element and remove the parts where the difference is 0.
If your array is named A, then:
A(diff(A)==0) = []
  8 件のコメント
Cyrus Tirband
Cyrus Tirband 2019 年 11 月 12 日
編集済み: Cyrus Tirband 2019 年 11 月 12 日
Hi Qiao,
You can use a similar approach. In this case, you have two requirements for the elements that you want to remove. Not only must the finite difference diff(A) be equal to zero (no repeating elements), the second finite difference, diff(diff(A)) must also be equal to zero (no change in slope). So, and(diff(A) ==0, diff(diff(A)) == 0) is the set that must be removed. Using de morgan's law this can be written a bit more succinctly as ~or(diff(A), diff(diff(A))).
Of course, you always want the first index to be included, so you need to enter that one manually as a 1, since diff(diff(A)) will be one index shorter.
In code:
A = [22; 22; 22; 24; 24; 24; 22; 22; 22];
A(~or(diff(A),[1; diff(diff(A))])) = []
A =
22
22
24
24
22
22
QIAO WANG
QIAO WANG 2019 年 11 月 13 日
Thank you Cyrus. Actually, before you replied to me, I also raised a question in the community and got a good one. Anyway, yours is a brilliant idea! Worked for me too.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by