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

2 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by