Locating adjacent identical elements in string array

1 回表示 (過去 30 日間)
dormant
dormant 2020 年 1 月 2 日
回答済み: Urmila Rajpurohith 2020 年 1 月 7 日
I want to find the positions and number of identical adjacent elements in a string array.
I worked out how to count the number of identical elements like this
DT = [ "all", "vt", "vt", "ro", "ro", "ro", "vt" ];
[uniqueDT,~,idx] = unique(DT,'stable');
count = hist(idx,unique(idx));
But I want to return an array that shows where adjacent alements are and how many, a bit like this (although the zeroes could be replaced by ones).
[ 1 2 0 3 0 0 1]

回答 (1 件)

Urmila Rajpurohith
Urmila Rajpurohith 2020 年 1 月 7 日
Currently MATLAB does not support any function that returns an array that shows : where the identical elements are present and their count.
From your code “idx” will return you the array where identical elements are present and you can add below line of code which will return the count of identical elements
y = arrayfun(@(t)nnz(DT==t), DT)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by