Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Is there a possibility to use varargin()?
1 view (last 30 days)
Show older comments
Is there any other way to perform such operation?
c = {rand,rand(4)};
z = @(...) mean(...);
z(c{:})
The same can be achieved using cellfun(), but is it possible only through anonymous function?
14 Comments
Walter Roberson
on 25 Aug 2019
What part would you like to see done in an anonymous function?
Is it the case that you have something that you would like to be able to write something like
z(SomeFunction(SomeArguments){:})
except for the fact that you cannot use {:} on the result of a function call?
Walter Roberson
on 25 Aug 2019
Some day it might be possible to define
C2S = @(C) struct('args', C);
z(C2S(SomeFunctionThatReturnsACell(SomeArguments)).args)
madhan ravi
on 25 Aug 2019
Edited: madhan ravi
on 25 Aug 2019
Exactly (https://in.mathworks.com/matlabcentral/answers/477479-is-there-a-possibility-to-use-varargin#comment_738726 ) sir Walter.
For instance blkdiag() can accept comma separated list as an argument likewise it would be lot cooler if any functions could do the same:
z = @(varargin) mean(varargin{:}); % this is just for illustraton purposes
c = {rand,rand(2)};
z(c{:}) % this would return the mean for each c content as a cell for example
Bruno Luong
on 25 Aug 2019
Edited: Bruno Luong
on 25 Aug 2019
What's wrong with using CELLFUN? Why you want to replace it with something else that is not designed for?
madhan ravi
on 25 Aug 2019
There's nothing wrong with it, just curious to perform such an operation.
Bruno Luong
on 25 Aug 2019
Well the answer is use CELLFUN
z = @(varargin) cellfun(@mean, varargin, 'unif', 0)
There is no valid reason asking anonymous function to replace cellfun.
I vote this question with -1
madhan ravi
on 25 Aug 2019
The point of the question is not to replace cellfun()! but to just imply varargin in anonymous function.
Bruno Luong
on 25 Aug 2019
Edited: Bruno Luong
on 25 Aug 2019
Sorry I don't see the point.
madhan ravi
on 25 Aug 2019
Edited: madhan ravi
on 25 Aug 2019
Thanks Bruno, I was exactly looking for a way , your comment (https://in.mathworks.com/matlabcentral/answers/477479-is-there-a-possibility-to-use-varargin#comment_738763 ) fullfilled it. How would you apply the same for the second dimension ?
madhan ravi
on 25 Aug 2019
Edited: madhan ravi
on 25 Aug 2019
https://in.mathworks.com/matlabcentral/answers/477479-is-there-a-possibility-to-use-varargin#comment_738728 - seems interesting sir Walter.
Walter Roberson
on 25 Aug 2019
"For instance blkdiag() can accept comma separated list as an argument"
Yes, and the calculation depends upon all of the inputs together.
"z(c{:}) % this would return the mean for each c content as a cell for example "
Which is a very different situation than blkdiag(), as the results would be independent of each other. For independent calculations, there is already cellfun if the input is a cell array.
In the case of comma separated lists like for blkdiag() you would need to think about the meaning of calls such as mean(1,2,3) . Is the result to be {mean(1), mean(2), mean(3)}? Or is it to be {mean(1) along the third dimension, mean(2) along the third dimension} ?
What would be a reasonable enhancement for a lot of routines including mean() would be to (for selected functions!) treat FUNCTION(CellArray, ...) as cellfun(@(ENTRY) FUNCTION(ENTRY,varargin{:}), CellArray, 'uniform', 0) . This should not be done for all functions, but it would make sense for a number of them.
madhan ravi
on 25 Aug 2019
Thank you for the reply sir Walter. Perhaps I should have simply asked by reprasing it as, is there any upsides of using varargin in an anonymous function? I have seen some answerers use varargin in an anonymous function which gave an advantage but cannot find any specific examples at the moment.
Walter Roberson
on 25 Aug 2019
Using varargin in an anonymous function is okay.
madhan ravi
on 27 Aug 2019
Thank you, sir Walter :)
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)