MATLAB: Could anyone explain the lefthand side of [~,si] = sort(sums);

syntax

I have seen syntax like this [~,si] = sort(sums); in many codes in the contest, but I don't understand.

Best Answer

In general, the tilde (~) means to ignore that output argument. sort() returns two output arguments: the sorted values and their location (indexes). So in your case it means to just get the indexes of the sorted items (where they occurred in the original, unsorted sums matrix), and not the sorted values themselves. If you had just one output argument, like this:
sortedValues = sort(sums);
then it would return the sorted values, not the locations. However, the programmer wanted the locations, and didn't care about the values so he/she had to have a second output argument to capture those locations because you can't do it this way:
indexes = sort(sums); % Not correct.
"indexes" would actually be the sorted values, not the indexes. The programmer didn't want the values themselves so they were essentially thrown away by using the tilde. You could have done it this way
[sortedValues indexes] = sort(sums);
but then you're having a variable hanging around, sortedValues, that you never plan on using and is just wasting memory. Does that explain it well?