Matrix multiplication and addition

조회 수: 4 (최근 30일)
Raheema Syed
Raheema Syed 2019년 2월 19일
답변: Star Strider 2019년 2월 19일
suppose i have two matrices:
a= [1 0 0 2 0 6
1 0 0 2 0 7
1 0 0 2 0 8
1 0 0 2 0 9
1 0 0 2 0 10]
b= [ 1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12]
i need to consider only 6th coloumn in my 'a' matrix i.e only [6;7;8;9;10]. size of 'a' matrix will be 5x1 now. And from 'b' matrix only 1st fours rows need to be considered. New size of 'b' matrix will be 4x5. Then, the following operation needs to be done between new 'a' and new 'b' matrix:
r= 6x1+7x2+8x3+9x4+10x5
s=6x2+7x3+8x4+9x5+10x6
t=6x3+7x4+8x5+9x6+10x7
u=6x4+7x5+8x6+9x7+10x8
sum=r+s+t+u
How should the matlab code be written for the above operation?

답변 (2개)

Kevin Phung
Kevin Phung 2019년 2월 19일
m1 = a(:,6);
m2 = b(1:4,:)';
prod = m1.*m2;
part_sums = sum(prod);
r = part_sums(1);
s = part_sums(2);
t = part_sums(3);
u = part_sums(4);
total_sum = r + s + t + u;
% or you can just do sum(sum(prod)) and skip the r s t u

Star Strider
Star Strider 2019년 2월 19일
If I understand correctly what you want to do, this works:
Result = sum(b(1:4,:) * a(:,6));
producing:
Result =
760

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by