Why does argmin index of NaN array have a value of 1 and not NaN?
2 ビュー (過去 30 日間)
古いコメントを表示
[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 件のコメント
Dyuman Joshi
2023 年 12 月 11 日
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.
採用された回答
その他の回答 (2 件)
Fangjun Jiang
2023 年 12 月 11 日
編集済み: Fangjun Jiang
2023 年 12 月 11 日
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 件のコメント
Torsten
2023 年 12 月 12 日
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
2023 年 12 月 12 日
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 件のコメント
Steven Lord
2023 年 12 月 12 日
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
2023 年 12 月 12 日
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?
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!