Compare nearby elements in array

조회 수: 13 (최근 30일)
Enthusiasten
Enthusiasten 2019년 12월 2일
댓글: Enthusiasten 2019년 12월 3일
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

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 2일
편집: KALYAN ACHARJYA 2019년 12월 2일
"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
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 3일
Yes, I agreed, if you can avoid multiple loops, it would be better.
Enthusiasten
Enthusiasten 2019년 12월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 12월 2일
I think the islocalmax function, specifying both the dimension over which to operate and a 'ProminenceWindow', will do what you want.
  댓글 수: 1
Enthusiasten
Enthusiasten 2019년 12월 3일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by