MATLAB Answers

Compare nearby elements in array

1 ビュー (過去 30 日間)
Enthusiasten
Enthusiasten 2019 年 12 月 2 日
Commented: 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

  0 件のコメント

サインイン to comment.

採用された回答

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 件のコメント

Enthusiasten
Enthusiasten 2019 年 12 月 3 日
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!
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.

サインイン to comment.

More Answers (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!

サインイン to comment.

サインイン してこの質問に回答します。

製品


リリース

R2015b

Translated by