How to calculate in one or many matrices based on other matrix?
1 view (last 30 days)
Show older comments
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
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.
Answers (1)
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.
1 Comment
Dyuman Joshi
on 12 Oct 2023
Nope, this is not a good answer.
DO NOT USE EVAL - https://in.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval#answer_236116
And neither does this answer give the output that OP wants to obtain.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!