MATLAB Answers

Yogesh Shukla

what does it mean by writing [~,idx] in code?

Asked by Yogesh Shukla
on 27 Apr 2013

for p= 4:4:population

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


best = routes(idx,:);

what idx, ~ means??

  1 Comment

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



No products are associated with this question.

2 Answers

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:

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.


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.

  1 Comment

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.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!