Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Adding the columns of matrix and concatenating them

조회 수: 1 (최근 30일)
Pranjali Priyadarshini
Pranjali Priyadarshini 2015년 4월 20일
마감: MATLAB Answer Bot 2021년 8월 20일
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
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
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.

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by