Compare nearby elements in array

15 views (last 30 days)
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

KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Dec 2019
Edited: KALYAN ACHARJYA on 2 Dec 2019
"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
KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Dec 2019
Yes, I agreed, if you can avoid multiple loops, it would be better.
Enthusiasten
Enthusiasten on 3 Dec 2019
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
Enthusiasten
Enthusiasten on 3 Dec 2019
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 Loops and Conditional Statements 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!