How to extract rows and columns of matrix that will not equal to 0.
조회 수: 3 (최근 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!