필터 지우기
필터 지우기

Sum of N matrices

조회 수: 2 (최근 30일)
Pedro Guevara
Pedro Guevara 2018년 7월 28일
편집: Pedro Guevara 2018년 7월 30일
The reason for this new message was to request a new favor.
Is there a code or function in matlab that allows me to add an undefined number (N) of matrices, which I already have a program? As an example I put the following situation to be understood better:
Aux = Mat (1) + Mat (2) + Mat (3) + .... + Mat (n)
I thank you for your attention and I hope you can help me with my predicament.
  댓글 수: 8
Pedro Guevara
Pedro Guevara 2018년 7월 28일
OK, I will try to make my code implementing indexing. Could you ask for help if you need it? I appreciate your attention.
Stephen23
Stephen23 2018년 7월 29일
편집: Stephen23 2018년 7월 29일
@Pedro Guevara: the approach using eval is specifically advised against by all experienced MATLAB users and by the MATLAB documentation, which has a whole page explaining why to avoid doing this. It states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
You have already already wasted more than one day trying to work with this inefficiently designed code, in contrast per isakson gave a much better solution in just a few seconds, which is simpler, neater, more efficient, less buggy, and easier to work with. This is one reason why you should rewrite your code: it will save you time! Do you see the pattern here? If you continue to design your code like this, then you will continue to fight your code for no real benefit. Or you could use arrays and indexing, which is what the MATLAB documentation recommends, and make developing, debugging, and running your code much faster.

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

채택된 답변

per isakson
per isakson 2018년 7월 29일
편집: per isakson 2018년 7월 29일
I've modified the example of my comment.
n = 12;
sz = [3,3];
MKG = nan( sz(1),sz(2), n );
for ix = 1 : n
MKG(:,:,ix) = ones(sz); % inv(M_Trans)*M_Kele*M_Trans;
end
sum( MKG, 3 )
outputs
ans =
12 12 12
12 12 12
12 12 12
>>
  댓글 수: 1
Pedro Guevara
Pedro Guevara 2018년 7월 30일
편집: Pedro Guevara 2018년 7월 30일
I think I love you. XD I am trying to understand your code by making it run step by step and apparently, with some modifications, you can help me with the purpose of my work. Thank you very much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by