Borrar filtros
Borrar filtros

What is the difference between [R,~]=size(M) and r0=Size(M,1)

1 visualización (últimos 30 días)
Soul Free
Soul Free el 1 de Sept. de 2018
Comentada: Walter Roberson el 22 de Dic. de 2018
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?

Respuestas (1)

ahmed nebli
ahmed nebli el 1 de Sept. de 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 comentarios
ahmed nebli
ahmed nebli el 1 de Sept. de 2018
no, i just tested it its the same! there is no diffrence
Walter Roberson
Walter Roberson el 22 de Dic. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by