How to extract rows and columns of matrix that will not equal to 0.

4 views (last 30 days)
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?

Answers (1)

Athul Prakash
Athul Prakash on 15 Mar 2021
Edited: Athul Prakash on 15 Mar 2021
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!

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!