从 MatLAB 数组中获取最接近零的值?
对于大家来说,这是一个有趣的(也可能是简单的)问题,我有两个数组,我需要确定真正的最小值(即最接近零的值)和真正的最大值(即距离零最远的值),无论该值是否为正数或负面。
为此,我计算了 Positive_max 和 Positive_min,以及 Negative_max 和 Negative_min,如下所示...
test = [3, 4, -2, -7, 6];
positive_min = min(test(test>=0)); %Should be 3
positive_max = max(test(test>=0)); %Should be 6
negative_max = min(test(test<=0)); %Should be -7
negative_min = max(test(test<=0)); %Should be -2
问题是,我现在需要将 Positive_min 与 Negative_min 进行比较,看看哪个最接近于零将 Positive_max 与 Negative_max 进行比较,看看哪个离零最远。我似乎不知道该怎么做...
我将不胜感激任何帮助!
An interesting (and probably simple) problem for you all, I have two arrays, and I need to determine the real minimum (i.e value closest to zero) and the real maximum (i.e. value furthest from zero) regardless of whether that value is positive or negative.
To do this, I have calculated the positive_max and the positive_min, as well as the negative_max and the negative_min as shown below...
test = [3, 4, -2, -7, 6];
positive_min = min(test(test>=0)); %Should be 3
positive_max = max(test(test>=0)); %Should be 6
negative_max = min(test(test<=0)); %Should be -7
negative_min = max(test(test<=0)); %Should be -2
Trouble is, I now need to compare the positive_min to the negative_min to see which is closest to zero, as well as comparing the positive_max to the negative_max to see which is furthest from zero. I can't seem to figure out how to do this...
I would appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
abs
函数给出
2< /code> 并
给出
7
。如果您想查找其中每个值的实际有符号值,可以使用 min< 中的第二个输出选项/a> 和 max:You could try using the
abs
functiongives
2
andgives
7
. If you want to find the actual signed value of each of these, you can use the second output option from min and max:Matlab 已经采用复数的 max() 或 min() 的绝对值。
因此,另一种快速方法是
min(test+1i)-1i
或max(test+1i)-1i
Matlab already takes the absolute value for max() or min() for complex numbers.
So another quick way to do this would be
min(test+1i)-1i
ormax(test+1i)-1i