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

for p= 4:4:population
dists= total_dist(rand_pair(p-3:p));
[~,idx]=min(dists);
best = routes(idx,:);
what idx, ~ means??

5 件のコメント

the cyclist
the cyclist 2013 年 4 月 27 日
Yogesh, I don't know why you posted this question four times, but I deleted the other three.
merlin toche
merlin toche 2023 年 3 月 10 日
Hello
anyone can help me with my code below? I want to do a classification using fuzzyknn.please, I tried to do what I can but as I'm still a beginner, I need your help to finalize this work. thank you for your advance here attached my code
Steven Lord
Steven Lord 2023 年 3 月 10 日
Since this doesn't sound like it's related to the original question about the [~, idx] syntax you should ask it as a separate question using the Ask link at the top of this page.
merlin toche
merlin toche 2023 年 3 月 13 日
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
merlin toche
merlin toche 2023 年 3 月 13 日
i have this error when i plot my code
attached

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2013 年 4 月 27 日
編集済み: James Tursa 2013 年 4 月 27 日

15 投票

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 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 13 日
Hello @James Tursa, do you have any source for the equivalence mentioned?
The webpage cited in the link doesn't refer to any such relation.
Adam Danz
Adam Danz 2023 年 3 月 13 日
@Dyuman Joshi, the page James shared in his answer is a good resource but doesn't contain James' analogous explanation.
When an output is suppressed with a tilde, the output is computed from within the function but isn't returned and therefore isn't stored in the caller's workspace. The only detail that differs from James' anaology is that the dummy variable is never returned to begin with, but it is computed within the min function.
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 14 日
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)
a = 5
b = 25
c = -5
out = -5
function [a,b,c]=yo(x)
a=x
b=x.^2
c=-x
end
Adam Danz
Adam Danz 2023 年 3 月 14 日
Right, and if you suppress the lines within the function,
[~,~,out]=yo(5)
out = -5
function [a,b,c]=yo(x)
a=x;
b=x.^2;
c=-x;
end

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2013 年 4 月 27 日
編集済み: the cyclist 2013 年 4 月 27 日

9 投票

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 件のコメント

James Tursa
James Tursa 2013 年 4 月 27 日
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.

サインインしてコメントする。

カテゴリ

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by