Deleted first repeated elements of an array

5 ビュー (過去 30 日間)
Tiago Miguel Cavaleiro Rodrigues
Tiago Miguel Cavaleiro Rodrigues 2019 年 12 月 2 日
編集済み: Adam Danz 2019 年 12 月 3 日
Hello, I am trying to process a data vector, ignoring the B first and C last samples of each trial. It should be the following:
A = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5] % data vector
B= 1 % delete n initial repeated values
C=2 % delete n final repeated values
Replacing unwanted data for -1 for example
A = [-1,1,1,1,1,-1,2,2,2,2,-1,3,3,3,3,-1,4,4,4,4,-,5,5,5,5] %removing the n = 1 first elements of each repeated streak
A= [-1,1,1,-1,-1,-1,2,2,-1,-1,-1,3,3,-1,-1,-1,4,4,-1,-1,-1,5,5,-1,-1] % removing the n=1 first and n = 2last elements of each repeated streak
Any help please?
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 12 月 2 日
Is the length of each "trial" the same and known? Or is the rule about "streaks" as implied in the comments?

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

採用された回答

Adam Danz
Adam Danz 2019 年 12 月 2 日
編集済み: Adam Danz 2019 年 12 月 3 日
Here's a demo. The first section just creates demo inputs. The second section solves your question. The third section just confirms that the output matches your expected output.
See inline comments for details
% Build inputs
A = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5];% % data vector
B = 1; % delete n initial repeated values
C = 2; % delete n final repeated values
%desired outcome (for comparison)
A_desired = [-1,1,1,-1,-1,-1,2,2,-1,-1,-1,3,3,-1,-1,-1,4,4,-1,-1,-1,5,5,-1,-1];
% Identify groups of consecutive values
da = cumsum([1,diff(A)~=0]);
aGroupIdx = arrayfun(@(x)find(da==x),unique(da),'UniformOutput',false);
% Replace the first B repeats with filler
% and the last C repeats with filler
filler = -1;
Anew = A;
for i = 1:numel(aGroupIdx)
Anew(aGroupIdx{i}(1:min(B,numel(aGroupIdx{i})))) = filler;
Anew(aGroupIdx{i}(max(1,numel(aGroupIdx{i})-C+1):end)) = filler;
end
% Check that it matches the expected outcome
isequal(A_desired, Anew)
  6 件のコメント
Tiago Miguel Cavaleiro Rodrigues
Tiago Miguel Cavaleiro Rodrigues 2019 年 12 月 3 日
It doesn't assume the values are increasing, but I thought it only worked for sets of different repeated numbers.
It wouldn't hold for example for the vector in blue.
A = [0,0,0,0,1,1,1,1,0,0,0,0,2,2,2,2,0,0,0,0]
But I just confirmed it does, sorry for all the trouble.
Thank you again
Adam Danz
Adam Danz 2019 年 12 月 3 日
編集済み: Adam Danz 2019 年 12 月 3 日
No worries. Like I said, I did correct a small but important mistake so I'm glad the discussion continued.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by