Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Adding the columns of matrix and concatenating them
조회 수: 1 (최근 30일)
이전 댓글 표시
I created about 900 matrices using the command genvarname. So it created matrices A1, A2,.... etc. Now, I want to sum the columns of each of these matrices and then combine them row wise. How do I do this in an efficient manner?
댓글 수: 1
Image Analyst
2015년 4월 21일
Explain in detail what "sum the columns of each of these matrices and then combine them row wise" means with a small number of small matrices. You can sum each column with
columnSums = sum(yourMatrix, 1);
but what does "combine them row wise" mean???
답변 (1개)
Michael Haderlein
2015년 4월 20일
편집: Michael Haderlein
2015년 4월 20일
Each week, there are about 5-10 questions with this respect. The simple answer is, "not this way". The only way is an extensive use of eval and that's precisely what I wouldn't do. It's like carefully avoiding the benefits of Matlab.
If it isn't too much effort, start creating your variables again and put them into an array, thus, instead of something like
myvarname=genvarname(repmat({'A'},1,5));
eval([myvarname{1} '=rand(13);'])
eval([myvarname{2} '=rand(13);'])
eval([myvarname{3} '=rand(13);'])
you better do something like
for cnt=1:900
A(:,:,cnt)=rand(13); %or whatever value the matrices have
end
You can also preallocate and improve performance this way. Then, summation of the columns is just
columnsum=sum(A,1);
The column sums are already combined row wise (if that's what you mean).
If this totally doesn't work for you because the creation of your variables took too much time, you can do something like
for cnt=1:900
columnsum(cnt,:)=eval(['sum(', myvarname{cnt} ')']);
end
But you'll have not much fun with debugging etc, so I really wouldn't recommend that.
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!