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

4 次查看(过去 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
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.
Michael
Michael 2023-12-11
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.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2023-12-11
编辑:Matt J 2023-12-11
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

更多回答(2 个)

Fangjun Jiang
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 = []
[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 个评论
Michael
Michael 2023-12-12
@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 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
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]
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 个评论
Steven Lord
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by