MATLAB: Plz help me to understand those cod of lines

digital image processing

[~, candidates_idx] = sort(candidates);
idx_median = candidates_idx(round(numel(candidates)/2))

Best Answer

Hi,
[~, candidates_idx] = sort(candidates);
gives indices for the sorted elements of the array candidats. For example if candidats would be:
candidates =
6 8 4 7 5
candidates_idx =
3 5 1 4 2
Then we know, that the smallest element of candidates is at index 3 of candidates. The biggest value we do find at index 2 in candidates.
The next line:
idx_median = candidates_idx(round(numel(candidates)/2))
at first:
>> numel(candidates)/2
ans =
2.5000
gives us the number of elements in candidates divided by 2. The result of this operation will be rounded to get an integer value:
>> round(numel(candidates)/2)
ans =
3
Then the value you got this way will be used as index for the array candidates_idx to get the value of candidates_idx at the position calculated. The answer will be the value of candidates x at position of the rounded number of elements in candidates divided by 2:
candidates =
6 8 4 7 5
candidates_idx =
3 5 1 4 2
idx_median =
1
This is what the lines do.