Compare nearby elements in array

Hello all,
i am currently working on a project and I need your help.
I have an array with around 500 x 500 elements. The elements are values from 0,1...255.
I need something to compare nearby elements in this array.
The comparsion should work like this:
for i=1:size(A)
for j=1:size(A)
If current_element>=Tresh & previous_element>=Tresh & next_element>=Tresh
B(i) = A(i);
elseif current_element>=Tresh & next_element>=Tresh & next_after_next_element>=Tresh
B(i) = A(i);
elseif second_to_last_element>=Tresh & last_element>=Tresh & current_element>=Tresh
B(i) = A(i);
elseif ... (the same with every columm)
else B(i,j) = 0;
end
for example:
A = [4 0 0 0 3; 6 0 0 9 2; 6 7 9 8 1; 6 0 0 6 4; 0 10 0 0 0];
the result should be:
B = [0 0 0 0 0; 6 0 0 9 0; 6 7 9 8 0; 6 0 0 6 0; 0 0 0 0 0];
this example already has a threshold which only indicates elements equal to or greater than 5
I tried different posibilites but could not find any solution for my problem. I read a lot about the "Game of Life"-problem but think this is not suitable for my problem.
What i have tried so far is following code:
A = [4 0 0 0 3; 6 0 0 9 2; 6 7 9 8 1; 6 0 0 6 4; 0 10 0 0 0];
B=double(A);
Thresh=5;
for i=1:size(A)
for j=1:size(A)
if A(i)>=Thresh & A(i+1)>=Thresh
B(i) = A(i);
elseif A(j)>=Thresh & A(j+1)>=Thresh
B(j) = A(j);
else B(i,j) = 0;
end
end
end
and the result is:
B = [0 0 0 0 0; 6 0 0 9 2; 6 7 9 8 1; 0 0 0 0 0 ;0 10 0 0 0]
As you can see, it almost works how I need it. Unfortunateley values above 9 are a problem because they are not replaced by 0 if possible.
What can I do to refer to the second to last, previous and next after next element?
I know that for example
elseif A(i)>=Thresh & A(i+1)>=Thresh & A(i+2)>=Thresh
and
elseif A(i)>=Thresh & A(i-1)>=Thresh & A(i-2)>=Thresh
will not work.
I know that '-2' or similar is not working due to logical issues. But why '+2' as well?
Thanks in advance

 Accepted Answer

"I have an array with around 500 x 500 elements"
Please note that you have mentioned about 2-D array , Here I have considered the previous elements and next element based on column number (considering same row),
data=randi(255,[500,500]);
Tresh=?? % Define
for i=2:499
for j=1:500
if data(i,j)>=Tresh && data(i-1,j)>=Tresh && data(i+1,j)>=Tresh
B(i,j)=A(i,j);
elseif
......% Hope you can implement it now
end
end
end

3 Comments

Thanks Kalyan for your answer!
This is the solution I was looking for. To avoid those logical issues, I have to set new limits. This was a thing, I have not considerd.
for i=3:498
for j=3:498
But is there anyway to consider the first two and last two elements without inserting and later deleting new rows and columns containg zeros?
I mean, I can do other loops like
for i=1
for i=1
if data(i,j)>=Tresh && data(i+2,j)>=Tresh && data(i+1,j)>=Tresh
B(i,j)=data(i,j);
elseif data(i,j)>=Tresh && data(i,j+2)>=Tresh && data(i,j+1)>=Tresh
B(i,j)=data(i,j);
else B(i,j)=0
end
end
end
and
for i=2
for i=2
if data(i,j)>=Tresh && data(i-1,j)>=Tresh && data(i+1,j)>=Tresh
B(i,j)=data(i,j);
elseif data(i,j)>=Tresh && data(i,j-1)>=Tresh && data(i,j+1)>=Tresh
B(i,j)=data(i,j);
else B(i,j)=0
end
end
end
and the same for the last two rows and columns. But it is not ressource-saving, is it?
Thanks for your help!
Yes, I agreed, if you can avoid multiple loops, it would be better.
Thanks for your help!
I have finally added two rows and columns at the beginning and the end.
After the loops, I have deleted them so i can evaluate every element during the loops.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 2 Dec 2019
I think the islocalmax function, specifying both the dimension over which to operate and a 'ProminenceWindow', will do what you want.

1 Comment

Thanks Steven for your answer!
I tried this function but it was not, what i exactly wanted.
But it might be helpful for an other problem of mine.
Thanks!

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!