pointing to an element in a returned function
古いコメントを表示
I'm not certain how to describe this question, so my title may have not been as descriptive as I would have liked. To start off, I know my way around matlab fairly well, so you are welcome to respond accordingly. Lets say that I have a function that returns a 1x5 array. ie.
x=Calc() % returns x=[1 4 2 6 4]
now I want to use the 4th element in x (x(4)=6)to perform a calculation, such as
y=x*10; % which returns y=60
is there a way to perform this calculation without defining x by pointing to the fourth element that would be returned by Calc()? as in
y=Calc().{element4}*10;
another use would be to say that I have a matrix x, and I want to return element (4,2) from the product of x and another variable. y=x*15{element (4,1)} % obviously I could say x(4,2)*15, but that's not the point.
Thanks Jason
回答 (2 件)
Walter Roberson
2011 年 9 月 15 日
There is no built-in method in MATLAB to do this.
Work-around:
indexat = @(expression,varargin) expression(varargin{:});
and likewise
indexcellat = @(expression,varargin) expression{varargin{:}};
then you could use
y = indexat(Calc(),4);
and
y = indexat(x*15,4,1);
David Young
2011 年 9 月 15 日
0 投票
The basic answer is "no" - you need the intermediate variable.
I have a feeling there was also a more recent discussion in Answers, but I can't find it now. Anyway, such discussions concern whether extending MATLAB to allow syntax such as Calc(x)(4) would be possible, practical or desirable in the future. For the present, you can't do it except in some special cases such as indexing into an element of a cell array.
1 件のコメント
David Young
2011 年 9 月 15 日
Ah - I think Walter's pointed to the discussion I couldn't find.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!