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!