Matrix multiplication and addition
显示 更早的评论
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
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
2019-2-19
If I understand correctly what you want to do, this works:
Result = sum(b(1:4,:) * a(:,6));
producing:
Result =
760
类别
在 帮助中心 和 File Exchange 中查找有关 Sparse Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!