How can I find minimum values from array in matlab?

a 夏天 提交于 2019-12-11 20:23:23

问题


I want to extract the two points (i.e 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

回答1:


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;




回答2:


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.



来源:https://stackoverflow.com/questions/33654943/how-can-i-find-minimum-values-from-array-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!