what does it mean by writing [~,idx] in code?
92 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sanuj Shukla
am 27 Apr. 2013
Kommentiert: Adam Danz
am 14 Mär. 2023
for p= 4:4:population
dists= total_dist(rand_pair(p-3:p));
[~,idx]=min(dists);
best = routes(idx,:);
what idx, ~ means??
5 Kommentare
merlin toche
am 13 Mär. 2023
please I posted the question as you asked so well but no answer. I went through this page because for 2 months my concerns have been ignored, can you explain this to me? thank you
Akzeptierte Antwort
James Tursa
am 27 Apr. 2013
Bearbeitet: James Tursa
am 27 Apr. 2013
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.
4 Kommentare
Dyuman Joshi
am 14 Mär. 2023
Thank you for the response, Adam.
I now know that while a variable that is suppressed is computed, but it is not returned and not stored in the caller's workspace.
I was stuck on an approach on how to show/understand it via code, but it was quite simple -
[~,~,out]=yo(5)
function [a,b,c]=yo(x)
a=x
b=x.^2
c=-x
end
Adam Danz
am 14 Mär. 2023
Right, and if you suppress the lines within the function,
[~,~,out]=yo(5)
function [a,b,c]=yo(x)
a=x;
b=x.^2;
c=-x;
end
Weitere Antworten (1)
the cyclist
am 27 Apr. 2013
Bearbeitet: the cyclist
am 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 Kommentar
James Tursa
am 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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
