MATLAB Answers

0

Find and edit interval of array when element is equal to a value?

Asked by Piscina99 on 8 Jul 2019
Latest activity Edited by Andrei Bobrov
on 9 Jul 2019
Hi everyone!
I have a small question.
I have an array that looks something like this [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2]
And I'd like to find those intervals of consectuve 2s that are longer than 3 elements. After that I find those interval, I want to cut the first elements untill they reach the length for 3 elements.
So my final array should be like this [0 0 0 0 2 2 2 0 0 0 0 0 0 0 2 2 2 0 0 0 0 2 2]
The first interval of 2s was made by 5 elements, now it's only 3 elements.
The second interval of 2s was made by 7 elements, now it's only 3.
The last one was made by 2 elements, it says the same.
Is it possible to do this?
Thank you very much and I hope you're having a nice day

  0 Comments

Sign in to comment.

Products


Release

R2019a

2 Answers

Answer by Jon
on 8 Jul 2019
Edited by Jon
on 8 Jul 2019
 Accepted Answer

Here is another approach, which basically relies on an earlier contribution from Jan with a small modifications for your problem. Please also see Jan's earlier contribution at https://www.mathworks.com/matlabcentral/answers/382011-how-to-count-the-number-of-consecutive-identical-elements-in-both-the-directions-in-a-binary-vecto
Maybe this approach is more efficient than utilizing string find, but you would have to do some timing tests to see. For small vectors performance may not be an issue.
x = [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2];
% start with idea from jan
% https://www.mathworks.com/matlabcentral/answers/382011-how-to-count-the-number-of-consecutive-identical-elements-in-both-the-directions-in-a-binary-vecto
d = [true, diff(x) ~= 0, true]; % TRUE if values change
n = diff(find(d)) ; % Number of repetitions
% now modify Jan's idea slightly for your application
% the even elements of n are the lengths of the runs of 2's
% clip them so do not exceed 3
n(2:2:end) = min(n(2:2:end),3);
% in preparation for using repelem, build a vector with alternating
% values of zero and 2
v = zeros(size(n));
v(2:2:end) = 2;
% build new vector with maximum run length 3
y = repelem(v, n);

  2 Comments

?
>> x = circshift([0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2],2)
d = [true, diff(x) ~= 0, true]; % TRUE if values change
n = diff(find(d)) ; % Number of repetitions
n(2:2:end) = min(n(2:2:end),3);
v = zeros(size(n));
v(2:2:end) = 2;
y = repelem(v, n)
x =
2 2 0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0
y =
0 0 2 2 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 2 2 2
>>
The attached revision, is slightly more general and covers the case where the sequence can start either with a zero or a non-zero. You could also easily extend further to the more general case of zero and non-zero elements, rather than specifically zeros and twos.
% % % x = [0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2];
x = circshift([0 0 2 2 2 2 2 0 0 0 2 2 2 2 2 2 2 0 0 0 0 2 2],2)
% start with idea from jan
% https://www.mathworks.com/matlabcentral/answers/382011-how-to-count-the-number-of-consecutive-identical-elements-in-both-the-directions-in-a-binary-vecto
d = [true, diff(x) ~= 0, true]; % TRUE if values change
n = diff(find(d)) ; % Number of repetitions
% now modify Jan's idea slightly for your application
% alternating elements of n give the lengths of the runs of zeros and twos but we
% need to determine whether the runs of twos are the odd or the even
% elements of n
if x(1) == 0
% sequence starts with zero, so even values of n are the lengths of the
% runs of twos
iStart = 2;
else
iStart = 1;
end
% alternating elements of n are the lengths of the runs of 2's
% clip them so do not exceed 3
n(iStart:2:end) = min(n(2:2:end),3);
% in preparation for using repelem, build a vector with alternating
% values of zero and 2
v = zeros(size(n));
v(iStart:2:end) = 2;
% build new vector with maximum run length 3
y = repelem(v, n)

Sign in to comment.


Answer by Andrei Bobrov
on 8 Jul 2019
Edited by Andrei Bobrov
on 9 Jul 2019

i1 = double(diff([A,0]) == -2);
ii = find(i1) - 3;
i1(ii(ii > 0)) = -1;
out = cumsum(i1,'revers').*A

  0 Comments

Sign in to comment.