Deleted first repeated elements of an array

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 Comment

Is the length of each "trial" the same and known? Or is the rule about "streaks" as implied in the comments?

Sign in to comment.

 Accepted Answer

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 Comments

I ended up implementing differently, assuming I would allways choose A and B to be smaller than the repeated values vector.
But your way is more robust, thanks! :)
remove_initial_trialtime = 150; % remove n first samples of each trial
remove_final_trialtime = 150; %remove n last samples of each trial
index = find(diff(conditionr)~=0); % find indicators where condition change
filler = -1;
for i = 1:length(index)
trial_corr(index(i)+1 : index(i)+remove_initial_trialtime) = filler;
trial_corr(index(i)-remove_final_trialtime : index(i) ) = filler;
end
Glad I could help!
The approach in your comment above produces different results from the ones described in your question, though. When I apply that approach to the example values, the outcome doesn't match your expected outcome. Just something to look into....
% I'm guessing at where to insert the example values....
conditionr = [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];%
trial_corr = conditionr;
remove_initial_trialtime = 1; % remove n first samples of each trial
remove_final_trialtime = 2; %remove n last samples of each trial
index = find(diff(conditionr)~=0); % find indicators where condition change
filler = -1;
for i = 1:length(index)
trial_corr(index(i)+1 : index(i)+remove_initial_trialtime) = filler;
trial_corr(index(i)-remove_final_trialtime : index(i) ) = filler;
end
% Compare results against exected results (from the question)
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];
[trial_corr(:),A_desired(:)]
ans =
1 -1
1 1
-1 1
-1 -1
-1 -1
-1 -1
2 2
-1 2
-1 -1
-1 -1
-1 -1
3 3
-1 3
-1 -1
-1 -1
-1 -1
4 4
-1 4
-1 -1
-1 -1
-1 -1
5 5
5 5
5 -1
5 -1
Thanks for pointing out that it is not working properly for the first and end trial!
My expected result was this:
Blue: condition of interest; Orange: Excluding portions of the trial.
Screenshot 2019-12-03 at 18.11.30.png
Just realized that using your implementantion, you assume that the 'condition' values are increasing and have different values. Which doesn't hold true in this case..
Is there an easy way to adapt it in this case?
Thanks again,
My answer doesn't assume that values are increasing. It works, for example, with this input:
A = [5.2,5.2,5.2,5.2,5.2,2,2,2,2,2,6,6,6,4,4,4,4,4,5,5,5,5,5];
I'm not sure what you mean by "you assume that the 'condition' values ... have different values". What does that mean?
I did notice one error in my answer, though. I corrected that error in the answer and it's shown below.
Anew(aGroupIdx{i}(max(1,numel(aGroupIdx)-C+1):end)) = filler; % OLD
Anew(aGroupIdx{i}(max(1,numel(aGroupIdx{i})-C+1):end)) = filler; % NEW
%--------------------------------------^^^
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 on 3 Dec 2019
Edited: Adam Danz on 3 Dec 2019
No worries. Like I said, I did correct a small but important mistake so I'm glad the discussion continued.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!