Is it possible to do multi index command in one line?
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
For example, if I want to get number of columns in an array I would have to do the following:
A = [1 2; 3 4];
mySize = size(A);
myCol = mySize(2);
It adds 2 lines and an extra variable in my code. Is it possible to put them in one line like:
myCol = size(A)(2);
Somthing similar to this because this is not the correct syntax.
Thank you very much!
0 commentaires
Réponse acceptée
Matt J
le 28 Avr 2021
Modifié(e) : Matt J
le 28 Avr 2021
Not in general, but in the case of your example there is a shorter syntax,
myCol=size(A,2)
2 commentaires
Steven Lord
le 28 Avr 2021
For some definition of "one line" it is. Write a function:
A = [1 2 3; 4 5 6];
sz = size(A) % for checking
y = getElement(size(A), 2) % The one line
But this has a drawback that the shorter syntax doesn't.
size(A, 3) % A has trailing singleton dimensions
y2 = getElement(size(A), 3)
function y = getElement(x, whichone)
y = x(whichone);
end
Plus de réponses (2)
Matt J
le 28 Avr 2021
An approximation of this can be enabled through some incredibly convoluted OOP methods
It's not worth it...
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!