How to ignore output variables in matlab?

[dummy index] = sort(A); 

I want to ignore the first output of the sort function and just save the indexes. When I use the above, I get a warning in the matlab editor that:

The value assigned to the mannequin is not used.

and instead, ~ is suggested. When i use ~.

 [~ index] = sort(A); 

I got the following error:

use ~ to ignore a value in this context is not valid.

Does anyone have a solution for this?

+4
source share
2 answers

You must add a comma and separate the output arguments to make ~ work.

Next works

 [dummy index] = sort(A); [dummy, index] = sort(A); [~, index] = sort(A); 

but

 [~ index] = sort(A); 

not executed.

+12
source

It really works, although your question doesn't even show you using ~, where you say you get an error.

 A = rand(1,5); [~,ind] = sort(A); ind ind = 3 5 1 2 4 
0
source

Source: https://habr.com/ru/post/1439283/


All Articles