Why does argmin index of NaN array have a value of 1 and not NaN?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
[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 Commenti
Dyuman Joshi
il 11 Dic 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.
Risposta accettata
Più risposte (2)
Fangjun Jiang
il 11 Dic 2023
Modificato: Fangjun Jiang
il 11 Dic 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 Commenti
Torsten
il 12 Dic 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
il 12 Dic 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 Commenti
Steven Lord
il 12 Dic 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
il 12 Dic 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?
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!