3 views (last 30 days)
Show older comments
kayne on 28 May 2011
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/8380-simplifying-modulus-code
Open in MATLAB Online
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Matt Fig on 28 May 2011
Open in MATLAB Online
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
kayne on 29 May 2011
⋮
Open in MATLAB Online
PLease bear with me as matlab is still new to me.
I dont understand how I am going to be able to define all the Star (*) point below. As for some of the Columns I need it to be different. Ie some are -1's, 1's and 4's,
Code *if Space(r,c) == 2 % The node that forms the condutor *elseif Space(r,c) == 1 % Node on the edge of the TL *elseif c ~= Columns % The Dielectric node that donot fall not on symmetry edge *else % The Dielectric node that falls on the symmetry edge
If I was to use your code I an not sure how I would difine something at a certain point.
Like if I this part of the code I need to define where the conductor is.
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;
This part tell me where the nodes are on the edge of the TL
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;
This tells me that the nodes are not on the line of symetry
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
And finally this is the node that fall on the symetry edge.
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
Can these all be replace with the code that you wrote?
B(2:end,[1 2 4 5]) = -1;
B(1:end-1,3) = 4;
If so how am I able to define these the different sections.
Thanks for your help on this I appricate it.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Matt Fig on 29 May 2011
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/8380-simplifying-modulus-code#comment_18091
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)
Sign in to comment.
kayne on 29 May 2011
Open in MATLAB Online
Thank for the head up Matt, I see what you mean about the code I had compare to yours.
I also understand why you said that "space" is just zeros. Something that I forgot to add which looking back I should have picked up was that I had define 'space' in an earlier part of my code which set particular nodes 2 and 1. I have added this below and also have simplified the code that I orignally posted.
This part of the program outline the space of the transmission line and
% assisn either a 1 if the node is on the sheild or a 2 if the node is the conductor.
for r = 1:Rows % For all the rows in the grid
for c = 1:Columns % For all the column in grid
x = c*h - h; % x position of the current node
y = r*h - h; % y position of the current node
if ((x<(2+T)&&(x>(2-T)))||(x<(3+T)&&(x>(3-T))))&&((y<(1.5+T)&&(y>(0.5-T)))),
Space(r,c) = 2; % So now the conductor nodes are set to 2
elseif (x<T)||(x<(5+T)&&(x>(5-T)))||(y<T)||(y<(2+T)&&(y>(2-T))),
Space(r,c) = 1; % So now the nodes on the edge of the TL are set to one
end
end
end
% 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((r-1)*Columns + c , 3) = 1; % Column 3 of B
b((r-1)*Columns + c) = ConVol;
elseif Space(r,c) == 1 % Node on the shield
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
% b((r-1)*Columns +c) = ShieldVol;
elseif c ~= Columns % The Dielectric node that does not fall not on symmetry edge
B(mod((r-1)*Columns + c -1 + d(1), Columns*Rows)+1, 1) = -1; % Column 2 of B
B(mod((r-1)*Columns + c -1 + d(2), Columns*Rows)+1, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -1 + d(4), Columns*Rows)+1, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -1 + d(5), Columns*Rows)+1, 5) = -1; % Column 5 of B
else % The Dielectric node that falls on the symmetry edge
%B((r-1)*Columns + c , 3) = 4; % Column 3 of B
end
end
end
A = spdiags(B,d,Rows*Columns,Rows*Columns); % Create sparse matrix A
So I have been trying to manupliate your code to try and solve
elseif c ~= Columns % The Dielectric node that does not fall not on symmetry edge
B(mod((r-1)*Columns + c -1 + d(1), Columns*Rows)+1, 1) = -1; % Column 2 of B
B(mod((r-1)*Columns + c -1 + d(2), Columns*Rows)+1, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -1 + d(4), Columns*Rows)+1, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -1 + d(5), Columns*Rows)+1, 5) = -1; % Column 5 of B
else % The Dielectric node that falls on the symmetry edge
%B((r-1)*Columns + c , 3) = 4; % Column 3 of B
end
end
end
but because B2 sets all columns [1,2,4,5] =-1 and[3] to 4 I cant get the code to mimick my B matrix in my orignal code.
Thanks again for the help I am learning heaps more.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and Arrays
Find more on Matrices and Arrays in Help Center and File Exchange
Tags
- modulus
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office