필터 지우기
필터 지우기

Concatenating multiple matrices

조회 수: 1 (최근 30일)
Andy McCann
Andy McCann 2012년 4월 29일
A function i'm working on involves a for loop creating a series of matrices all of which need to be stored and then summed once the for loop has finished.
My initial thought was by concatenating all the matrices i could store all the data, however, the usual cat(dim, A, B) wouldn't work as the number of concatenations changes between scenarios so i was hoping someone could help.
Essentially i need to run the for loop, which produces an output matrix for each run through the loop, and then sum all the output matrices, no matter how many there are.
Any help would be greatly appreciated.
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2012년 4월 29일
Post an example: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

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

답변 (2개)

per isakson
per isakson 2012년 4월 29일
Does all the data fit in memory? Are all the matrices of the same size? If that is the case
M = nan( n1, n2, number_of_matrices );
for ii = 1 : number_of_matrices
number_of_matrices( :,:, ii ) = ...
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 4월 29일
I think you mean M on the left hand side rather than number_of_matrices.

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


Walter Roberson
Walter Roberson 2012년 4월 29일
Store them in a cell array, say A. Then
dimno = ndim(A{1}) + 1;
sum( cat(dimno, A{:}), dimno )
Do you definitely need to access the arrays after the loop, or do you just need their sum? If you just need their sum, you could do a running total.
  댓글 수: 2
Andy McCann
Andy McCann 2012년 4월 29일
I just need to sum them. This is great. Thanks!
Andy McCann
Andy McCann 2012년 4월 29일
Hi Sorry but i'ts still not working out for me,
if the matrix produced at the end of the run of the for loop is A, then does :
dimno = ndim(A{1}) + 1;
sum( cat(dimno, A{:}), dimno )
need to lie outside of the for loop?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by