How can I separate the elements of a vector ?

1 view (last 30 days)
Rayan Glus on 27 May 2022
Commented: Rayan Glus on 27 May 2022
Hello,
I have a row vector i of size say 1x300 and that its indices can be any number of the range [1 5] in a descending order. For example:
i = [5 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 0]
As you can notice number 4 is not always in i. And I'm trying to separate the similar numbers into 4 groups in this example.
for jj = max(i):-1:0
t = i==jj;
idx = find(t~=0);
.
.
.
end
In this particular example, jj should take only values 5,3,2,1,0. But it is not since the step is -1. And the resulting idx is empty.
When i is like the following, my code works fine.
i = [6 6 5 4 4 4 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 0]
I would appreciate your help.
0 CommentsShowHide -1 older comments

Sign in to comment.

Accepted Answer

Stephen23 on 27 May 2022
for jj = i(diff(i)~=0)
or
for jj = unique(i,'stable')
10 CommentsShowHide 9 older comments
Rayan Glus on 27 May 2022
My bad.
Thank you so much. I really appreciate it

Sign in to comment.

More Answers (1)

Walter Roberson on 27 May 2022
File Exchange, run length encoding. If I recall correctly, contribution is from Jan.
1 CommentShowHide None
Rayan Glus on 27 May 2022
Thanks Walter. I will check it out.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!