How to transform the entry in the vector in ascending and descending order after few points?

1 view (last 30 days)
Hi,
This question may sound silly but I really need help.
I have a vector
X=[1 1 1 1 1 10 1 1 1 1 1 1 1 1 1];
and i wish to transform this vector X to
X_new=[9 9 9 9 9 10 10 10 10 10 11 11 11 11 11];
by using the information and position of the entry 10 in vector X.
May I know how to construct the new vector X_new?
Thanks in advance.
  4 Comments
Ming Gui
Ming Gui on 22 Oct 2017
Hi Mr Çevikalp Sütunç,
Thanks for your answer. Unfortunately, the code can only fit the current X. What if I increase the number of sample in X? Let say
X=[1 1 1 1 1 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
The pattern remain the same, the new vector
X_new=[9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 13 13 13 13];
How do I have a universal code to fit for different sample?
Thanks a lot.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 22 Oct 2017
Edited: Andrei Bobrov on 22 Oct 2017
ii = find(X~=1)-1;
Y = zeros(size(X));
Y(1:ii:numel(X)) = 1;
out = X(ii + 1) + cumsum(Y) - 2;
or
ii = find(X~=1)-1;
out = ceil((1:numel(x))/ii) + X(ii+1) - 2;

More Answers (1)

Birdman
Birdman on 20 Oct 2017
clear;clc;X=[1 1 1 1 1 10 1 1 1 1 1 1 1 1 1];cnt=0;
for i=1:1:length(X)
if(X(i)==10 && cnt==0)
for j=1:1:i-1
X(j)=10-1;
end
cnt=1;
end
if(cnt==1)
if(i<=10)
X(i)=10;
end
end
if(i>10)
X(i)=11;
end
end

Tags

Community Treasure Hunt

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

Start Hunting!