Simplifying MODULUS code
5 views (last 30 days)
Show older comments
I have some code that I would like to know if it can be smiplified if anyway to reduce the amount of lines I am using.
I used the modulus command in Matlab to get the following but wanted to know if this can be simplified using another command?
I have to fill certain columns with number for a sparse matrix
%I have just added these into here in my program they are automatic but if i can work it out for these then I should be ok with the rest.
Columns = 6
Rows = 5
r = 5 % This is where I have set up the space
c = 6 % This is where I have
ConVol = 10; % This is the conductor voltage
ShieldVol = 0; % This is the voltage around the edge of the TL
Space = zeros(Rows,Columns); % The area of the transmission line
B = zeros(Rows*Columns,5);% Creates 5 coloumns in B filled with zeros
b = zeros(Rows*Columns,1);% Creates 1 column b filled with zeros
v = zeros(Rows*Columns,1); % Creates 1 column filled with zeros
% Sets up d with a diagonal index vector for a sparse matrix
d = [Columns 1 0 -1 -Columns];
for r = 1:Rows % For every row in grid
for c = 1:Columns % For every column in grid
if Space(r,c) == 2 % The node that forms the condutor
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns + c) = ConVol;
elseif Space(r,c) == 1 % Node on the edge of the TL
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns +c) = ShieldVol;
elseif c ~= Columns % The Dielectric node that donot fall not on symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
else % The Dielectric node that falls on the symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
end
end
end
A = spdiags(B,d,Rows*Columns,Rows*Columns); % Create sparse matrix A
Hopefully this question will make sense I am not sure how else I can post it.
Thanks
0 Comments
Answers (4)
Walter Roberson
on 28 May 2011
I'm not sure, but it looks to me as if you would benefit from either sub2ind() or ind2sub()
0 Comments
Matt Fig
on 28 May 2011
The way you have defined B (and Space), your FOR loops are equivalent to these:
for r = 1:Rows % For every row in grid
for c = 1:Columns % For every column in grid
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1;
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1;
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1;
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1;
end
end
This produces the same A as you have. In fact the FOR loops could be replaced entirely by:
B(2:end,[1 2 4 5]) = -1;
B(1:end-1,3) = 4;
0 Comments
kayne
on 29 May 2011
1 Comment
Matt Fig
on 29 May 2011
Note that I said, "The way you have defined B (and Space)..."
You defined Space as an array of zeros, so no element will ever be 1 or 2. If you keep Space as an array of zeros, then you don't need these IF conditions because they cannot possibly be met.
Once those are eliminated, the remaining conditionals are mutually exclusive, but have the same body of code within! So whether c is equal to Columns or not, the same code gets executed.
Just run your code and look at B at the end. Then run this:
B2 = zeros(size(B));
B2(2:end,[1 2 4 5]) = -1;
B2(1:end-1,3) = 4;
isequal(B,B2)
See Also
Categories
Find more on Matrices and Arrays 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!