What is the difference between [R,~]=size(M) and r0=Size(M,1)
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
ahmed nebli
le 1 Sep 2018
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 commentaires
Walter Roberson
le 22 Déc 2018
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.
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!