MATLAB: Replace elements of array with elements of another array without for loop arrayreplace 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? Best Answer [B,ia,ic] = unique(A)Third output argument ic will give you what you want. Related SolutionsMATLAB: Counting n subsequent element of an array b = conv(a,[1 1 1],'valid') MATLAB: Relabel the arrays’ elements with an increasing order Try thisA = [3 3 3 3 1 1 4 4 4 4 2 2 2];[~, ~, B] = unique(A, 'stable');Result>> BB = 1 1 1 1 2 2 3 3 3 3 4 4 4 Related QuestionGroup a Array ElementsFind missing element between two matrixConcatenating cell array elementsHow to create an array from a given array with the following criteria.Removing specific elements from arrayI want to sum specific elements in an a 2D matrixElements processing in array
Best Answer