Checking for multiple values that are the same in a vector

16 ビュー (過去 30 日間)
elspeth storey
elspeth storey 2019 年 7 月 10 日
コメント済み: Andrei Bobrov 2019 年 7 月 10 日
I have a vector of data values and would like to flag up if there are several identical values in a row.
If the data were A=[1,2,3,4,4,4,4,5,6,7,8] I would want it to flag up that there were 4 fours so that I could correct the values.
Is there a way of doing this other than a for loop with checking if A(2)==A(1)
Ideally it would only flag up an error if more than 10 were the same in a row (my real data is a lot larger than A).
Thanks
  2 件のコメント
madhan ravi
madhan ravi 2019 年 7 月 10 日
Explicitly show how your result should look like.
elspeth storey
elspeth storey 2019 年 7 月 10 日
if 10 or more values are both identical and consecutve
C=[1,1,2,3,2,1,3,2,2,2,2,2,2,2,2,2,2,2,3,2,1,1,3] so here, it would show that there was an error in the data because there are 11 twos in a row.
I would then want to set C(8:19) as new values. e.g. C(8:19)=x, where x=[2,3,1,2,1,2,3,1,1,1,2]
Thanks

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 7 月 10 日
C = C(:);
d = [1;diff(C) ~= 0];
ii = cumsum(d);
counts = accumarray(ii,1);
k = find(counts >= 10);
lo = ismember(ii,k);
n = sum(lo);
C(lo) = randi([1 3],n,1);
  2 件のコメント
madhan ravi
madhan ravi 2019 年 7 月 10 日
+1
Andrei Bobrov
Andrei Bobrov 2019 年 7 月 10 日
Thanks Madhan.

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 7 月 10 日
B=[1,2,3,4,4,4,4,5,6,7,8] ;
[a,b]=hist(B,unique(B)) ;
[b' a']
  2 件のコメント
elspeth storey
elspeth storey 2019 年 7 月 10 日
This provides data for a histogram format, but is there a way of seeing if the repeated values are next to each other in the vector?
KSSV
KSSV 2019 年 7 月 10 日
REad about unique. This gives indices also...

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

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by