Replace elements of array with elements of another array without for loop

A = [1 1 1 3 3 3 4 8 8 10 10 15 15]
B = unique(A)
B = [1 3 4 8 10 15]
I want the output to be [1 1 1 2 2 2 3 4 4 5 5 6 6]
I tried for loop :
for i=1:length(A)
A(A == B(i)) = i;
end
How can I do it without for loop?

 채택된 답변

Birdman
Birdman 2018년 3월 28일
편집: Birdman 2018년 3월 28일
[B,ia,ic] = unique(A)
Third output argument ic will give you what you want.

댓글 수: 1

So really in this case, since the first two outputs are not needed:
[~, ~, B] = unique(A);

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

태그

질문:

2018년 3월 28일

댓글:

2018년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by