Is it possible to do multi index command in one line?
조회 수: 29 (최근 30일)
이전 댓글 표시
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
채택된 답변
Matt J
2021년 4월 28일
편집: Matt J
2021년 4월 28일
Not in general, but in the case of your example there is a shorter syntax,
myCol=size(A,2)
댓글 수: 2
Steven Lord
2021년 4월 28일
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
추가 답변 (2개)
Matt J
2021년 4월 28일
An approximation of this can be enabled through some incredibly convoluted OOP methods
It's not worth it...
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!