简体   繁体   中英

How can I find minimum values from array in matlab?

图片

I want to extract the two points (ie their values) which are marked with black outline in figure. These minima points are 2 and 5. Then after extraction these marked points coordinates I want to calculate the distance between them.

The code that I am using to plot average values of image, calculate minimas and locations is

I1=imread('open.jpg');
I2=rgb2gray(I1);
figure, title('open');
plot(1:size(I2,1), mean(I2,2));
hold on
horizontalAverages = mean(I2 , 2);
plot(1:size(I2,1) , horizontalAverages)
[Minimas locs] = findpeaks(-horizontalAverages) 
plot(locs , -1*Minimas , 'r*')

Minima

-86.5647
-80.3647
-81.3588
-106.9882
-77.0765
-77.8235
-92.2353
-106.2235
-115.3118
-98.3706

locs =

    30
    34
    36
    50
    93
    97
   110
   121
   127
   136

It is a bit unclear from your question what you are actually looking for, but the following one liner will get you the local minima:

% Some dummy data
x = 1:11;
y = [3 2 1 0.5 1 2 1 0 1 2 3];

min_idx = ([0 sign(diff(y))] == -1) & ([sign(diff(y)) 0] == 1);

figure
plot(x, y);
hold on;
scatter(x(min_idx), y(min_idx))
hold off;

在此处输入图片说明

Use the 'findpeaks' function, if you have the signal processing toolbox.

[y,locs]=findpeaks(-x) 

will find the local minima. This function has a ton of options to handle all kinds of special cases, so is very useful.

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