Usage of tilde as output
Show older comments
i have understood that tilde ignores the output but what does tilde do here,please explain me the logic behind this,
for k=1:n
[~, m]=max(abs(U(k:n,k)))
end
1 Comment
@suman: You have understood, that the tilde ignores an output and ask for what the tilde does. Azzi and Walter reply, that the tilde ignores an output. This seems to be a tautological strategy.
I've move the code from the title to the body of the question.
Answers (2)
Azzi Abdelmalek
on 14 Jan 2013
Edited: Azzi Abdelmalek
on 14 Jan 2013
[a,b]=max(sin(1:100))
% a is the maximum value
% b is the corresponding index
~ is used to ignore a ( to avoid using memory).
3 Comments
James Tursa
on 15 Jan 2013
Edited: James Tursa
on 15 Jan 2013
The use of ~ does not avoid using memory. It simply throws away the corresponding output (somewhat cleaner looking code). It avoids assigning the output to a variable and then manually clearing that variable ... but it does not avoid creating the output in the first place (that would have to take place inside the function itself). I believe the behavior of avoiding the memory usage in the first place for some functions is on the list of future MATLAB enhancements.
Azzi Abdelmalek
on 15 Jan 2013
Edited: Azzi Abdelmalek
on 15 Jan 2013
When you clear a variable, I think it's avoid using memory
Walter Roberson
on 15 Jan 2013
The memory is still used temporarily, only to be thrown away again afterwards.
Consider for example,
[~, fs] = wavread('SomeSound.wav');
wavread() does not know that the first output is being thrown away, so it will go trough all the trouble of decoding the sound and storing it and returning it. And then it will be thrown away.
[~, b] = max(...)
is the equivalent in MATLAB of
[a, b] = max(...)
clear a
except for using a variable name that is not otherwise used (and probably not needing an actual entry in the workspace, which only matters if the workspace already has 65533 entries in it.)
Walter Roberson
on 14 Jan 2013
The tilde there ignores the first output of max(), which is the value of the maximum. The second output of max is left intact; it is the index of the maximum.
Because "m" is being overwritten in every iteration of the loop, the code is equivalent to the loop-less
[~, m] = max(abs(U(n, n)))
which would return 1 as it is max() of a single element.
3 Comments
Nicholas Bitler
on 24 Nov 2020
I see tilde everywhere in Matlab, how do you make one with a typical keyboard?
Rik
on 24 Nov 2020
That depends on what you mean by 'typical'. On my keyboard it is a dead key just under escape (shift-backtick). 'Dead key' in this context means that hitting it doesn't do anything until I type another key. This allows the formation of ã and ñ.
Nicholas Bitler
on 25 Nov 2020
And there it is...Thanks Rik
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!