for loop in if statement, changing value of matrix element depending on its nearby elements
1 view (last 30 days)
Show older comments
In matrix 'a' i want to convert all its 24 elements to 0 if the middle element has 20 more value than its other 24 elements and leave the middle element as it is. If anyone of the 24 elements does not satisfy the condition, i want it to leave the values of all 25 elements as it is.
clc;
clear;
a= [0.8147 0.0975 0.1576 0.1419 0.6557; 0.9058 0.2785 0.9706 0.4218 0.0357; 0.1270 0.5469 60 0.9157 0.8491; 0.9134 0.9575 0.4854 0.7922 0.9340; 0.6324 0.9649 0.8003 0.9595 0.6787];
if
for k=1:1:2
for l=1:1:2
a(i,j)-a(i+k,j+l)>20;
a(i,j)-a(i-k,j-l)>20;
a(i,j)-a(i+k,j-l)>20;
a(i,j)-a(i-k,j+l)>20;
a(i,j)-a(i,j+l)>20;
a(i,j)-a(i,j-l)>20;
a(i,j)-a(i-k,j)>20;
a(i,j)-a(i+k,j)>20;
end
end
a(i,j)=a(i,j);
for k=1:1:2
for l=1:1:2
a(i+k,j+l)=0;
a(i-k,j-l)=0;
a(i+k,j-l)=0;
a(i-k,j+l)=0;
a(i,j+l)=0;
a(i,j-l)=0;
a(i-k,j)=0;
a(i+k,j)=0;
end
end
0 Comments
Answers (1)
Walter Roberson
on 5 Dec 2021
Subtract the middle number from the entire matrix. If all of the results are less than -20 then the middle was at least 20 more than the other values.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!