How to extract rows and columns of matrix that will not equal to 0.
4 次查看(过去 30 天)
显示 更早的评论
A = 5 3 2 4 1
5 3 2 4 1
5 3 2 4 1
5 3 2 4 1
5 3 2 4 1
B = x
y
0
0
0
F = 0
-20
F3
F4
F5
F=A*B
I have this set of matrix equation. I would like to reduce my vector size of A when the row of B=0, so that I can have 2x2 matrix to solve the x&y of B. How do I reduce the size using nested loops?
so far, i have this set of code:
mats = [ 1 0 0 0 -20;
2 1 1 0 0;
3 1 1 0 0;
4 1 1 0 0];
nodeBC = []; % create empty matrix for boundary condition, where there is 0 row of matrix B
for i=1:Nn
nodeBC = [nodeBC;mats(i,2);mats(i,3)] % adding BC in x and y in the same vector.
end
A_f = []; % reduced stiffness matrix
for i=1:2*Nn
for j=1:2*Nn
if nodeBC(i) ==0 && nodeBC(j) ==0
A_f = A_f+A;
end
end
end
how do i create this A_f with reduced 2x2?
0 个评论
回答(1 个)
Athul Prakash
2021-3-15
编辑:Athul Prakash
2021-3-15
Hey Teb.
When programming in MATLAB, it is best to avoid for loops and vectorize the code as far as possible.
If you have F=A*B and want to solve for elements of B, you may try the left-division operator
B = F\A;
See the documentation here:
You may simplify and remove zero-elements from 'B' as follows:
newA = A(:,B~=0);
newB = B(B~=0);
B~=0 outputs a boolean array where 1's denote the locations of 'B' that are not '0'. This logical array is used for indexing into 'B' as well as the columns of 'A'. You may refer to 'Logical Indexing' in MATLAB.
Hope it helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!