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
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