for loop in if statement, changing value of matrix element depending on its nearby elements
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2021-12-5
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!