i need to find peaks and valleys in a large array and store them in another array. help?
Show older comments
my code so far is this
load('elevation_data');
e=elevation_data;
maxarray=[];
minarray=[];
[n,m]=size(elevation_data);
for j=2:n-1
for k=2:m-1
if elevation_data(j,k)>max([e(j-1,k),e(j-1,k-1),e(j-1,k+1),e(j,k+1),e(j,k-1),e(j+1,k-1),e(j+1,k),e(j+1,k-1)]);
elevation_data(j,k)=maxarray;
elseif elevation_data(j,k)<min([e(j-1,k),e(j-1,k-1),e(j-1,k+1),e(j,k+1),e(j,k-1),e(j+1,k-1),e(j+1,k),e(j+1,k-1)]);
elevation_data(j,k)=minarray;
end
end
end
Answers (2)
Matt J
on 26 Oct 2012
0 votes
Could you just use imregionalmax() and imregionalmin()?
4 Comments
Daniel
on 26 Oct 2012
If you have to use loops, your code thus far looks pretty close. The only problem I see is
elevation_data(j,k)=maxarray;
elevation_data(j,k)=minarray;
Since maxarray and minarray are always empty, I don't know what those lines are supposed to accomplish. Why would you even be trying to modify elevation_data(j,k)? You're trying to produce a new array based on it.
Daniel
on 26 Oct 2012
One thing you could do is to first pre-allocate minarray and maxarray as arrays the same size of elevation_data
minarray=nan(size(elevation_data));
maxarray=nan(size(elevation_data));
You haven't said what should go in the places which are neither mins nor maxs, so I assume them to be NaN.
Then in the loop, you assign the min/max value to these arrays instead of to elevation_data.
Andrei Bobrov
on 26 Oct 2012
as imregionalmax and imregionalmin :
A = elevation_data;
a1 = inf(size(A) + 2);
a2 = -a1;
a1(2:end-1,2:end-1) = A;
a2(2:end-1,2:end-1) = A;
s = size(A);
i1 = bsxfun(@plus,(1:s(1))',(0:s(2)-1)*(s(1)+2));
i2 = bsxfun(@plus,(0:2).',(0:2)*(s(1)+2));
idx = bsxfun(@plus,i1(:),i2(:).');
idx1 = idx(:,5);
idx2 = idx(:,[1:4,6:end]);
mins = reshape(all(bsxfun(@le,a1(idx1),a1(idx2)),2),size(A));
maxs = reshape(all(bsxfun(@ge,a2(idx1),a2(idx2)),2),size(A));
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!