Why does argmin index of NaN array have a value of 1 and not NaN?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael
el 11 de Dic. de 2023
Comentada: Matt J
el 12 de Dic. de 2023
[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
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.
Respuesta aceptada
Más respuestas (2)
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]=min([inf,nan])
[M, I]=min([nan,inf])
[M,I]=min([nan nan])
[M,I]=min([inf inf])
7 comentarios
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.
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]
[minvalue, minindex] = min(x)
x(minindex) % Current behavior
x(NaN) % Your proposed behavior
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
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
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?
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!