필터 지우기
필터 지우기

Doubt about matrices mxm

조회 수: 1 (최근 30일)
Pedro Guevara
Pedro Guevara 2018년 7월 18일
댓글: James Tursa 2018년 7월 26일
Good afternoon.
I am new to the forum and new to programming with matlab, and I request your help with a topic:
I have the following code lines:
M_Kele_G=inv(M_Trans)*M_Kele*M_Trans;
M_Kele_global =['MK_G',int2str(f),' = M_Kele_G'];
eval(M_Kele_global)
where the variable "f" has an initial value of 1, it is a variable that grows one unit and that is contained in a for cycle. The result shown for a certain data entry:
MK_G1 =
500 0 0 -500 0 0
0 120 120 0 -120 120
0 120 160 0 -120 80
-500 0 0 500 0 0
0 -120 -120 0 120 -120
0 120 80 0 -120 160
If I in the matlab editor add the following line of operation: MK_G1 + MK_G1 matlab does the correct operation by adding both matrices, however I require some code that allows me to operate different matrices that vary according to the value of "f" , something like the following multiplication of matrices:
'MK_G',int2str(f) * 'MK_G',int2str(f)
I would appreciate your help in this regard. Thank you very much.
  댓글 수: 1
Stephen23
Stephen23 2018년 7월 18일
"I am new to the forum and new to programming with matlab, and I request your help..."
The best advice you will get is to avoid creating or accessing variable names dynamically. Dynamically creating or accessing variable names is how beginners force themselves into writing slow, complex, buggy code (like you have). You can easily avoid doing this by using indexing.

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2018년 7월 18일
편집: James Tursa 2018년 7월 18일
See the following link:
In short, you should rewrite your code to use cell arrays or nD arrays instead of using the variable naming scheme you are currently doing, which as you can already see makes it difficult to write good code.
E.g., instead of this mess:
for i=1:N
eval(['MyMatrix' num2str(i) '= MyFunction(MyVariable' num2str(i) ');']);
end
You can do something like this instead:
for i=1:N
MyMatrix{i} = MyFunction(MyVariable{i});
end
A lot nicer!
  댓글 수: 2
Pedro Guevara
Pedro Guevara 2018년 7월 26일
Thank you all for the reply. I already solved, in part, the problem I had, but now I have another. I have this line of code:
prueba=['MK_G', num2str(px(j,1)),'(4:6,4:6)'];
but I require, in some way, that the intervals of the matrix that I am evaluating (in this case 4: 6,4: 6) are in some way variable. I thank you for your collaboration with this new problem.
James Tursa
James Tursa 2018년 7월 26일
Again, don't name variables dynamically like MK_G1, MK_G2, etc. Instead, use cell arrays or nD arrays. E.g., with cell arrays you could just to this if you wanted to get at the contents of the variable:
x = something
y = something
prueba = MK_G{px(j,1)}(x:y,x:y);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by