Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Adding the columns of matrix and concatenating them

1 Ansicht (letzte 30 Tage)
Pranjali Priyadarshini
Pranjali Priyadarshini am 20 Apr. 2015
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentar
Image Analyst
Image Analyst am 21 Apr. 2015
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???

Antworten (1)

Michael Haderlein
Michael Haderlein am 20 Apr. 2015
Bearbeitet: Michael Haderlein am 20 Apr. 2015
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.

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by