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

New to MATLAB?

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

Asked by Yogesh Shukla

Yogesh Shukla (view profile)

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??

1 Comment

the cyclist

the cyclist (view profile)

on 27 Apr 2013

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

Yogesh Shukla

Yogesh Shukla (view profile)

Tags

Products

No products are associated with this question.

2 Answers

Answer by James Tursa

James Tursa (view profile)

on 27 Apr 2013
Edited by James Tursa

James Tursa (view profile)

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

0 Comments

James Tursa

James Tursa (view profile)

Answer by the cyclist

the cyclist (view profile)

on 27 Apr 2013
Edited by the cyclist

the cyclist (view profile)

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

James Tursa

James Tursa (view profile)

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.

the cyclist

the cyclist (view profile)

Contact us