To detect repeated values in an array in MATLAB, you can use the unique
function along with the diff
function:
main.m153 chars5 lines
In the above code, we first use unique
function to get unique values from array arr
, and then use diff
function on the index of unique array to find out the repeated values. find
function is used to get the index of repeated values in the original array. Finally, repeated_vals contains the repeated values.
Alternatively, if you want to know if any element is repeated in an array, you can use length(unique(arr)) ~= length(arr)
as a condition. This will return true
if there are repeated elements in the array.
gistlibby LogSnag