필터 지우기
필터 지우기

How can I separate the elements of a vector ?

조회 수: 4 (최근 30일)
Rayan Glus
Rayan Glus 2022년 5월 27일
댓글: Rayan Glus 2022년 5월 27일
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.

채택된 답변

Stephen23
Stephen23 2022년 5월 27일
for jj = i(diff(i)~=0)
or
for jj = unique(i,'stable')
  댓글 수: 10
Stephen23
Stephen23 2022년 5월 27일
"do you know by any chance why when jj reaches 0, i get updated and the for loop starts all over again"
I don't see that (nor do I see your code):
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,-1,-1,-1];
for jj = unique(i(i>=0),'stable')
display(jj)
end
jj = 6
jj = 5
jj = 4
jj = 3
jj = 2
jj = 1
jj = 0
Rayan Glus
Rayan Glus 2022년 5월 27일
My bad.
Thank you so much. I really appreciate it

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 27일
File Exchange, run length encoding. If I recall correctly, contribution is from Jan.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by