What does a tilde (~) inside square brackets mean?

[~, Palette] = kmeans(reshape(B(:),M*N,3),8,'E','s','S','U');
Specifically, what does the ~ inside the square brackets represent (e.g. a matrix with multiple LHS assignment)?

 Réponse acceptée

Tanguy
Tanguy le 18 Avr 2013
The function kmean can be used with two outputs :
[IDX,C] = kmeans(X,k)
Here you use the brackets to define the two outputs (it is not an array). Here, IDX will be the first output (a vector containing the cluster indices of each point) and C will be the second one (a matrix containing the cluster centroid locations).
So the brackets don't mean that you have just one ouput (an array), but they are used to gather the outputs.
And whatever happens, IDX will be the first output and C the second one.
But if you just want to know C (and you don't care IDX), it is not usefull to assign this value to a variable.
So, when you use [~,palette], that means that you just want the second output of your function, and do not care the first one.
Use this is helpfull for you (you don't have many useless variables in your workspace), and is faster!

7 commentaires

Prb
Prb le 9 Mai 2019
Hi Very nice explanation of [~]. I am wondering what is an equivalent of this in Python. Could you please help me here.
There is no equivalent in Python. Python cannot return to multiple variables.
You need to design each Python function to return an Object (equivalent to MATLAB struct), or a tuple (which is immutable), or a list.
Whatever you decide, you could put None into the first output location. However, there still needs to be something there to receive the entire set of outputs and pull out the remaining wanted values.
@Prb In python we use the underscore operator, `_`, as a throwaway variable. For instance:
import numpy as np
a = np.random.randn(5, 5) + 1j*np.random.randn(5, 5)
u, s, _ = np.linalg.svd(a, full_matrices=True)
Here we do not want to use the last matrix returned by the SVD (doc here) later in the code, so instead of using a dummy variable, we use `_`.
Does this '~' save the time for calculating IDX, or it calculates both IDX and C but only output C?
I want to know whether it actually saves time.
Thanks!
@Prb: there is no equivalent in Python to MATLAB's multiple output arguments. Consider what is returned by fun0 as an input to fun1:
fun1(fun0())
In MATLAB the first output argument only will be passed. In Python it will be the entire tuple/list/whatever defined as the output.
In MATLAB the number of requested output arguments can be counted (i.e. nargout) and the output/functionality of a function changes with this number (e.g. calendar, find, ndgrid). Python has no equivalent to this.
The use of tilde in MATLAB or underscore (by tradition) in Python has no bearing on this.
Does this '~' save the time for calculating IDX, or it calculates both IDX and C but only output C?
As far as the community experts have been able to tell, the implementation is that an output slot is still created for the ~ variables, but that no symbol table entry is created and the output is released afterwards -- the same way that for 1 + 2 + 3 an output slot must be created for the (1+2) part but the slot is unnamed and will be released.
As far as the community experts have been able to find, there is no documented method or flag on a variable that could be used by a function to determine that a particular ~ output is going to be thrown away.
You can use nargout to probe what the index of the last programmed output is, but you cannot tell whether any of them are ~ or not.
Adam Danz
Adam Danz le 3 Jan 2021
> Does this '~' save the time for calculating IDX, or it calculates both IDX and C but only output C?
This discussion focuses on that topic:
TL;DR: no but there are ways to communicate which output variables should be produced in your function.

Connectez-vous pour commenter.

Plus de réponses (2)

It is equivalent to
[temp, Palette] = kmeans(reshape(B(:),M*N,3),8,'E','s','S','U');
clear temp

2 commentaires

Delvin
Delvin le 18 Avr 2013
What is "temp" ? Is it short for temporary as in temporary variable? Also, what does the square brackets mean ie is this an array ? Thanks
[ThIsVArIAblEiZnOTuzED, Palette] = kmeans(reshape(B(:),M*N,3),8,'E','s','S','U');
clear ThIsVArIAblEiZnOTuzED
and the [] mean that multiple outputs are being returned from the function. It is not an array.

Connectez-vous pour commenter.

Ankur Bhardwaj
Ankur Bhardwaj le 24 Mai 2017

0 votes

Whether it is supported in Matlab Version 2009 or not.

1 commentaire

Steven Lord
Steven Lord le 24 Mai 2017
This functionality was introduced in release R2009b. So it depends what you mean by "Version 2009" -- release R2009a no, release R2009b yes.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by