I am looking for a function that can be utilized for adding several matrix, something like as what SIGMA from 1....n where the n is 1,2,3,...n. but here I want to add matrix.
Alternatively, I can do this with loop but I have been concerned about being time-consuming computationally. is there any effective and fast way to do this, please let me know.

 채택된 답변

Guillaume
Guillaume 2015년 7월 3일

0 개 추천

It all depends on how your matrices are stored.
The worst case, they're all stored as individual variables. A bad idea to start with. Transform them into one of the other two options.
Second option, is they're all stored in a cell array. The simplest thing would be to concatenate them all into a single matrix of a higher dimension and apply sum on that:
matrices = {rand(5,5,5); rand(5,5,5); rand(5,5,5)}; %store 3x 3D matrices
higherdim = ndims(matrices{1}) + 1;
matricessum = sum(cat(higherdim, matrices{:}), higherdim)
The last option it that they're already stored all together in a matrix of a higher dimension, in which case, just apply the sum function:
matrices = cat(4, rand(5,5,5), rand(5,5,5), rand(5,5,5)); %store 3x 3D matrices
matricessum = sum(matrices, 4);

댓글 수: 4

I am not sure how should treat with the following matrices in my program.
cosabc = [cosa(:)'; cosb(:)'; cosc(:)'];
cos0abc = [cosa0(:)'; cosb0(:)'; cosc0(:)'];
a = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cosabc,1,3,TotNu));
a = bsxfun(@times,a,reshape(Cyabs,1,1,TotNu));
a = a + bsxfun(@times,eye(3),reshape(fy,1,1,TotNu));
b = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cos0abc,1,3,TotNu));
b = bsxfun(@times,b,reshape(Czabs,1,1,TotNu));
b = a - b;
Ex = zeros(3,TotNu);
for k=1:TotNu
Ex(:,k) = a(:,:,k) * SAp(:,k) + b(:,3,k); % nD matrix multiply
end
RondF_RondEta = reshape(a(:,:,TotNu:-1:1),3,[]);
RondF_RondZeta = reshape(b(:,:,TotNu:-1:1),3,[]);
Ex = Ex(:);
How store them in a higher dim matrices etc?
Guillaume
Guillaume 2015년 7월 4일
Them being which matrices?
Mohammad
Mohammad 2015년 7월 4일
편집: Mohammad 2015년 7월 4일
Those are matrices A and B that need to be calculated as follows.
A(:,:,1)=a(:,:,1)+a(:,:,2)+......a(:,:,28)
A(:,:,2)=a(:,:,29)+a(:,:,302)+......a(:,:,56)
.
.
.
.
A(:,:,m1)=a(:,:,.........
and similarly for b
B(:,:,1)=b(:,:,1)+b(:,:,2)+......b(:,:,28)
B(:,:,2)=b(:,:,29)+b(:,:,302)+......b(:,:,56)
.
.
.
.
B(:,:,m1)=b(:,:,.........
so It seems I need to apply changes like this following
for k=1:m1
Ex(:,k) = A(:,:,k) * SAp(:,k) + B(:,3,k); % nD matrix multiply
end
TotNu=28*m1
It would have been much simpler if you'd explained that in your question. You're trying to sum together some pages of the same matrix. Use reshape to put together all the pages you want to sum:
assert(mod(size(A, 3), 28) == 0); %otherwise it's not possible
A = sum(reshape(a, size(A, 1), size(A, 2), 28, []), 4)
%same for B

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 7월 3일

댓글:

2015년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by