MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Yogesh Shukla on 27 Apr 2013

for p= 4:4:population

dists= total_dist(rand_pair(p-3:p));

[~,idx]=min(dists);

best = routes(idx,:);

what idx, ~ means??

*No products are associated with this question.*

Answer by James Tursa on 27 Apr 2013

Edited by James Tursa on 27 Apr 2013

Accepted answer

The ~ represents an output that is discarded. It is essentially equivalent to:

[dummy,idx]=min(dists); clear dummy

For this example, the code wants to work with the index of the minimum value, not the value itself, so the minimum value that is returned is discarded and only the index is retained.

http://www.mathworks.com/help/matlab/matlab_prog/ignore-function-outputs.html?searchHighlight=tilde

Answer by the cyclist on 27 Apr 2013

Edited by the cyclist on 27 Apr 2013

When you see

>> [a,b,c] = function(...)

then a,b, and c are the output of a function. If you do not want one of the outputs of a function, then you can replace it with the ~ symbol:

>> [a,~,c] = function(...)

and then b will not be output.

James Tursa on 27 Apr 2013

To clarify, the syntax doesn't actually prevent the function from producing the output ... it just causes MATLAB to ignore the output and automatically clear it instead of assigning it to a workspace variable. So using the syntax makes your code cleaner looking but the function will still use the same resources (time & memory) to run.

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/73735#comment_145803

Yogesh, I don't know why you posted this question four times, but I deleted the other three.