简体   繁体   中英

how to find minimum value in a large matrix

I have a matrix Z eg

Z = randi(6, 20)

I want to find the minimum value from this matrix and display it using x,y coordinates. 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. 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. 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 . 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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