Clear Filters
Clear Filters

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

4 views (last 30 days)
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

Accepted Answer

Adam
Adam on 31 Aug 2017
Edited: Adam on 31 Aug 2017
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 Comments
Daniel Fiuza Dosil
Daniel Fiuza Dosil on 31 Aug 2017
Edited: Daniel Fiuza Dosil on 31 Aug 2017
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 on 31 Aug 2017
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.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 31 Aug 2017
Edited: John D'Errico on 31 Aug 2017
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 on 31 Aug 2017
Edited: Cam Salzberger on 31 Aug 2017
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 Comments
Daniel Fiuza Dosil
Daniel Fiuza Dosil on 31 Aug 2017
You are right, that seems more time consuming and complex than writing two lines. But really interesting answer! Thanks a lot Cam.
José-Luis
José-Luis on 31 Aug 2017
Good. Learned something today. Didn't know about subsref. That is one tortured one-liner. :)

Sign in to comment.

Categories

Find more on Matrices and Arrays 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!