Borrar filtros
Borrar filtros

Why does argmin index of NaN array have a value of 1 and not NaN?

4 visualizaciones (últimos 30 días)
[M, I] = min([NaN, NaN])
produces
M =
NaN
I =
1
Why? When there is no minimum (M=NaN) there should be no index returned for the minimum (the output variable I should also be NaN). This seems odd behaviour.
  2 comentarios
Dyuman Joshi
Dyuman Joshi el 11 de Dic. de 2023
Because that's how it is defined.
From the documentation of min - "If all elements in the operating dimension are missing, then the corresponding element in M is missing."
M is the output array here.
Michael
Michael el 11 de Dic. de 2023
In this case, I'm interested in the I output of the min function:
Index, returned as a scalar, vector, matrix, multidimensional array, or table. I is the same size as the first output.
When "linear" is not specified, I is the index into the operating dimension. When "linear" is specified, I contains the linear indices of A corresponding to the minimum values.
If the smallest element occurs more than once, then I contains the index to the first occurrence of the value.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 11 de Dic. de 2023
Editada: Matt J el 11 de Dic. de 2023
I imagine it is so that the logical test tf (below) will always return true. If the I output were NaN, an error message would result.
A=[NaN, NaN];
[M, I] = min(A);
tf=isequaln(M, A(I)) %We want this to be true, always
ans = logical
1

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 11 de Dic. de 2023
Editada: Fangjun Jiang el 11 de Dic. de 2023
Maybe the index is indeed returned by matching the min value to the input vector. Why does it matter? What is the significance in the case of min([nan nan])?
[M, I]=min([])
M = [] I = []
[M, I]=min([inf,nan])
M = Inf
I = 1
[M, I]=min([nan,inf])
M = Inf
I = 2
[M,I]=min([nan nan])
M = NaN
I = 1
[M,I]=min([inf inf])
M = Inf
I = 1
  7 comentarios
Michael
Michael el 12 de Dic. de 2023
@Torsten, in the case of my application, the NaNs originally arise because a NaN array is assigned using NaN(). Then the array is partially filled in with values where values should be. It is an intentional part of the code design. Thanks for your note of caution and for commenting on the question.
@Walter Roberson thanks for the historical background, this is interesting to know!
Torsten
Torsten el 12 de Dic. de 2023
in the case of my application, the NaNs originally arise because a NaN array is assigned using NaN(). Then the array is partially filled in with values where values should be. It is an intentional part of the code design.
If you have "control" over your NaN values, I apologize for my provocative comments.

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 12 de Dic. de 2023
If the second output from min in the case where the input is all NaN values were NaN, every single call to min that wanted to use that second output as an index into the input would have to guard themselves against the all-NaN input case using isnan. With the current behavior, that second output is always* usable as an index.
x = [NaN, NaN]
x = 1×2
NaN NaN
[minvalue, minindex] = min(x)
minvalue = NaN
minindex = 1
x(minindex) % Current behavior
ans = NaN
x(NaN) % Your proposed behavior
Array indices must be positive integers or logical values.
Why is minindex equal to 1? Well, since all the elements are the same 1 is as good as any other index. And there are other places in MATLAB where we default to the first dimension / element / etc. (functions that accept a dim argument and get passed a scalar and no dimension as input, for example.)
* There may be a case where it's not, involving very tall sparse matrices and linear indices. But I don't remember off the top of my head what that does; I'd have to double-check. That might just throw an error.
  3 comentarios
Steven Lord
Steven Lord el 12 de Dic. de 2023
I was speaking colloquially. I should have been more precise. All the elements are NaN. There's no inherent reason based on the value of the NaN elements to favor one over another. We could have chosen to return the index of the first element, the last element, the middle element, or any of the elements in the array. I suspect that it was Cleve's decision to keep it simple and just use the first element.
Matt J
Matt J el 12 de Dic. de 2023
Why is minindex equal to 1? Well, since all the elements are the same 1 is as good as any other index. And there are other places in MATLAB where we default to the first dimension
It's also the most efficient choice. Why update a register when you don't have to?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by