Info
此问题已关闭。 请重新打开它进行编辑或回答。
Is there a possibility to use varargin()?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2019-8-25
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
2019-8-25
Some day it might be possible to define
C2S = @(C) struct('args', C);
z(C2S(SomeFunctionThatReturnsACell(SomeArguments)).args)
madhan ravi
2019-8-25
编辑:madhan ravi
2019-8-25
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
2019-8-25
编辑:Bruno Luong
2019-8-25
What's wrong with using CELLFUN? Why you want to replace it with something else that is not designed for?
Bruno Luong
2019-8-25
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
2019-8-25
The point of the question is not to replace cellfun()! but to just imply varargin in anonymous function.
madhan ravi
2019-8-25
编辑:madhan ravi
2019-8-25
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
2019-8-25
编辑:madhan ravi
2019-8-25
https://in.mathworks.com/matlabcentral/answers/477479-is-there-a-possibility-to-use-varargin#comment_738728 - seems interesting sir Walter.
Walter Roberson
2019-8-25
"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
2019-8-25
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.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)