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!