How to skip elements of matrix in a loop?
3 views (last 30 days)
Show older comments
Hi
If I have a matrix defined as:
V = zeros(20,20);
%sides at 1,-1
V(1:21,1) = 1;
V(1:21,21) = -1;
V(1:21,2:20) = 2;
%zero potential triangle
V(14,8:13) = 0;
V(13,9:13) = 0;
V(12,10:13) = 0;
V(11,11:13) = 0;
V(10,12:13) = 0;
V(9,13:13) = 0;
(two metal plates with v=1 and v=-1 and a triangle in the middle v=0)
I am implementing the jacobi method by:
for j=2:column_size-1;
for i=2:row_size-1;
if V(i,j)~=1 & V(i,j)~=-1 & V(i,j)~=0 %update
V_new(i,j) = (V(i-1,j)+V(i+1,j)+V(i,j-1)+V(i,j+1))/4;
delta_V_new = delta_V_new+abs(V_new(i,j)-V(i,j));
else
V_new(i,j) = V(i,j); %keep initial conditions
end;
end;
end;
The loop is not converging properly. I want to keep the same boundary conditions..is there a way to skip the elements of the triangle in the middle without using the if statement? I am fine with using the if statementfor the 1 and -1
Thanks
0 Comments
Answers (1)
Image Analyst
on 28 Nov 2018
Just make up a mask with true or false where you want it to process, or skip. Then
for j=2:column_size-1;
for i=2:row_size-1;
if ~mask(i, j)
% Mask is false here, so skip rest of the loop.
continue;
end
% Else mask is true, so do the rest of the loop.
end
end
The "false" region(s) shape mask can be whatever shape you want, but the overall size should be row_size by column_size.
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!