remove repeating values in matlab

To remove repeating values from an array in MATLAB, you can use the unique() function.

Here's an example:

main.m
A = [1 2 3 3 4 5 5 5 6];
B = unique(A);
disp(B);
49 chars
4 lines

This will output: [1 2 3 4 5 6]

The unique() function returns a new array with only the unique values of the input array. By default, the function sorts the output array in ascending order. If you want to keep the original order, you can use an additional output argument:

main.m
[A_unique, idx] = unique(A, 'stable');
39 chars
2 lines

The idx array contains the indices of the input array that correspond to the unique values in A_unique.

related categories

gistlibby LogSnag