Is there any possible way of writing the following two-line code in one single line?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel Fiuza Dosil
el 31 de Ag. de 2017
Editada: John D'Errico
el 31 de Ag. de 2017
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
0 comentarios
Respuesta aceptada
Adam
el 31 de Ag. de 2017
Editada: Adam
el 31 de Ag. de 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 comentarios
Daniel Fiuza Dosil
el 31 de Ag. de 2017
Editada: Daniel Fiuza Dosil
el 31 de Ag. de 2017
Adam
el 31 de Ag. de 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.
Más respuestas (2)
John D'Errico
el 31 de Ag. de 2017
Editada: John D'Errico
el 31 de Ag. de 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.
0 comentarios
Cam Salzberger
el 31 de Ag. de 2017
Editada: Cam Salzberger
el 31 de Ag. de 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);
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 comentarios
José-Luis
el 31 de Ag. de 2017
Good. Learned something today. Didn't know about subsref. That is one tortured one-liner. :)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!