What is the difference between [R,~]=size(M) and r0=Size(M,1)
조회 수: 2 (최근 30일)
이전 댓글 표시
I know the second one returns the length of the 1st dimension which equals to number of rows and the first one ignores the columns as an output, but is there any difference if M has 2 dimensions?
댓글 수: 0
답변 (1개)
ahmed nebli
2018년 9월 1일
the output of the first one is 2 numbers, each number representing the size of the dimension, and the output of the second one will be just one number which is the number of rows.
댓글 수: 3
Walter Roberson
2018년 12월 22일
When you call size() passing in only an array, then the output depends upon the number of outputs you request.
If you request only one output, then the output is a vector of the dimensions, with at least two elements being returned, and more if needed, ending with the last dimension that is not 1.
If you request more than one output, then the value for each output except the last is the length of the corresponding dimension (e.g., third output corresponds to third dimension); but the last output will be the product of all of the remaining sizes. The rule is that the product of all of the returned elements will equal the number of elements in the array.
The case
[W,~]=size(A)
follows that rule about multiple outputs. It is equivalent to doing
[W, AnInternalVariableNameThatIsNotUsed] = size(A)
clear AnInternalVariableNameThatIsNotUsed
so W will store the length of the first dimension, and AnInternalVariableNameThatIsNotUsed would store the product of the length of all of the other dimensions, and then you would throw away AnInternalVariableNameThatIsNotUsed, leaving you with W storing the length of the first dimension.
When you call size() passing in an array and also a positive integer, then MATLAB extracts only that particular dimension and returns it. In the case of size(A,1) that would be only the first dimension.
So what difference is there between
[W,~] = size(A);
compared to
W = size(A,1);
The answer is that in both cases, W will end up storing the same value, but that the first of those will take longer to execute, since it has to extract the dimensions and multiply them out and then throw away that calculated value.
참고 항목
카테고리
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!