How to calculate in one or many matrices based on other matrix?

1 view (last 30 days)
Dear all,
I'm having these two matrices:
A = [ In - I2 B = [ C1 + 2G2 0
- I1 2G1 0
- I3 - 2G1 - 2G2 0
- I1 0 C2
- I4 0 2G3
- I2 ] 0 -2G3 ]
And I want to make a for loop to subtraction the same elements of matrix A and matrix B will subtraction with the same row of matrix A.
Example:
A(1,:) = In - I2 so I A(1,:) - A(6,:) = In - I2 - (- I2) = In and i want B do the same way.
A(6,:) = - I2
The result is like this:
A = [ In B = [ C1 + 2G2 2G3
0 2G1 -C2
-I3 - 2G1 - 2G2 0
-I4 ] 0 2G3 ]
Please give me some suggests and functions. Thank you very much.
  4 Comments
Jan
Jan on 27 Apr 2022
I do not understand this sentence: "And I want to make a for loop to subtraction the same elements of matrix A and matrix B will subtraction with the same row of matrix A."
If you post the code you use to create A and B, it is much easier to create an answer.
Nguyen Hoang Hai
Nguyen Hoang Hai on 27 Apr 2022
I defined variables and created matrices like this:
syms In I1 I2 I3 I4 C1 C2 G1 G2 G3
A = [In - I2;-I1;-I3;-I1;-I4;-I2]
B = [C1+2*G2,0;2*G1,0;-2*G1-2*G2,0;0,C2;0,2*G3;0,-2*G3]
Example I check elements of matrix A, A(1,:) = In - I2 and A(6,:) = - I2, both of them include element I2, and then I do subtraction to get the result A(1,:) - A(6,:) = In - I2 - (- I2) = In. Base on that, matrix B do the same subtraction with B(1,:) and B(6,:) at the same time.

Sign in to comment.

Answers (1)

SAI SRUJAN
SAI SRUJAN on 12 Oct 2023
Hi Nguyen Hoang Hai,
I understand that you are facing an issue in performing the same row operations on different matrices.
You can follow the below given example to resolve the issue.
syms In I1 I2 I3 I4 C1 C2 G1 G2 G3
A = [In - I2;
-I1;
-I3;
-I1;
-I4;
-I2];
B = [C1 + 2*G2, 0;
2*G1, -C2;
-2*G1 - 2*G2, 0;
0,C2;
0, 2*G3;
0, -2*G3];
m=["A","B"];
for i=1:length(m)
arr=m(i);
% performing A(1,:)=A(1,:)-A(6,:);
text = arr+ "(1,:)="+arr+"(1,:)-"+arr+"(6,:);";
eval(text);
end
With the help of the above example you can perform same row operations on different matrices.You can use the "eval" function to evaluate a MATLAB expression.For a comprehensive understanding of the "eval" function in MATLAB, please refer to the provided documentation below.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!