简体   繁体   English

如何在大矩阵中找到最小值

[英]how to find minimum value in a large matrix

I have a matrix Z eg 我有一个矩阵Z

Z = randi(6, 20)

I want to find the minimum value from this matrix and display it using x,y coordinates. 我想从此矩阵中找到最小值,并使用x,y坐标显示它。 I got it working once trying to work out the max value, but then replaced max with min in order to work out minimum value. 尝试计算出最大值后,我得到了它的工作,但后来用min代替了max以得出最小值。 The max works sometimes but the min has never worked, so I'm guessing something is incorrect. 最大值有时会起作用,但最小值从未起作用,因此我猜有些错误。 I need the min to work! 我需要分钟上班!

[x,y]=find(Z==max(max(Z)))
Z_max=Z(x,y))

[x,y]=find(Z==min(min(Z)))
Z_min=Z(x,y))

But I always get an error saying Index exceeds matrix dimensions. 但是我总是收到一个错误,说Index exceeds matrix dimensions. When it worked, it gave me answer like this (which is exactly what i want): 当它起作用时,它给了我这样的答案(这正是我想要的):

x =

     5


y =

     3


Z_max =

    6

Any suggestions will be very helpful. 任何建议将非常有帮助。

You need to use the three-ouput version of find . 您需要使用find的三输出版本。 Besides, that also returns the minimum (not only the indices): 此外,它还返回最小值(不仅是索引):

[x y min_Z] = find(Z==min(Z(:)));

This will give several values if the minimum is achieved at several entries. 如果在多个条目中达到最小值,则将提供多个值。 If you only want the first, use: 如果只想要第一个,请使用:

[x y min_Z] = find(Z==min(Z(:)),1);

A possibly faster alternative, which gives only the first: 可能更快的替代方法,它仅提供第一个:

[min_Z k] = min(Z(:));
[x y] = ind2sub(size(Z),k);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM