Is there any possible way of writing the following two-line code in one single line?

8 次查看(过去 30 天)
Hello all,
I was wondering if it possible to index the solution of a function once you are calling it. The output of the function is a vector and I want to obtain only the n-value of this output.
Example:
S = size(image) % eg. S, the size of the image could be [2046 1807 5]
S3 = S(3) % S3 = 5
What I was trying to do in one single line:
S3 = size(image)(3)
I have been running lately with the need of writing two lines of code instead of one in cases similar to this one. I am not aware of any way of writing this in one single line, but I hope that there is one, otherwise it would be practical to have it implemented.
PS: Suggestion for a better question title are welcome.
Daniel

采纳的回答

Adam
Adam 2017-8-31
编辑:Adam 2017-8-31
In a general case, no, you cannot index the result of a function call on a single line. This particular function just happens to have an overload that gives what you want directly.
S = size( image, 3 )
as is shown in
doc size
  2 个评论
Daniel Fiuza Dosil
Daniel Fiuza Dosil 2017-8-31
编辑:Daniel Fiuza Dosil 2017-8-31
In the case of a function different from:
size()
What's the most efficient way in terms of speed and memory of writing something like that? I may not want to save or calculate all the elements in the function output vector, but only one. I am trying to think a simple example for a case like this... (eg. like if size() wouldn't have that extra feature).
I guess it also depends on the function you are using?
Adam
Adam 2017-8-31
Yes, it depends on your function.
A lot of functions accept scalar or vector inputs, allowing you to calculate on only one element if you wish, but also allowing fast vectorised results over a whole vector if you desire this also.
It is impossible to give an answer in a generic case. Some functions are just so fast and their results take so little memory that it really doesn't matter to calculate an excess of results to take only one. Others are completely the opposite, but in those cases you are better finding or creating a different function that only operates on what you want in that case.

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2017-8-31
编辑:John D'Errico 2017-8-31
In the case of size, it has a second argument that answers your problem (as asked) directly.
However, nothing stops you from writing your own helper function, if this is something you do often.
takenth = @(X,n) X(n);
Or, you can save it as an m-file on your search path. Then it will still be there the next time you use MATLAB.
function Xn = takenth(X,n)
% takenth - extracts the nth element(s) of vector X
% % usage: Xn = takenth(X,n)
%
% arguments:
% X - a vector argument of any class (for which indexing is defined)
% n - positive integer that may not exceed the dimensions of X
%
% Note that if X is an array, then takenth will extract
% the nth element as it is held in memory.
Xn = X(n);
end
Now you can apply it to any output, even when that function does not have the ability.
takenth(primes(10),3)
ans =
5
takenth(primes(20),[3 5 6])
ans =
5 11 13
A virtue of tools like MATLAB is if they lack an ability that you often wish to use, you can write it yourself, thus extending and customizing the language to be as you like.

Cam Salzberger
Cam Salzberger 2017-8-31
编辑:Cam Salzberger 2017-8-31
For the size function, you can just provide the index of the size you want as a second argument to size. That's built-in and specific to that function though.
S = size(image, 3);
In general though, you would probably need to use subsref explicitly. Something like:
subsref(magic(3),struct('type','()','subs',{{2}}))
That's usually far more trouble than it's worth though. Only time I ever use it is if I absolutely positively must create an anonymous function that involves indexing.
Hope that helps!
-Cam
  2 个评论
Daniel Fiuza Dosil
Daniel Fiuza Dosil 2017-8-31
You are right, that seems more time consuming and complex than writing two lines. But really interesting answer! Thanks a lot Cam.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by