查看完整版本 : [MATLAB基础] matlab矩阵相乘的问题
tianya6421
2009-04-26, 10:29
A=[1 2 3 4];
B=[1 2;2 3;3 4;4 5]
让AB中行相乘
要达到A中的1 分别乘以B中的 1 2
A中的2分别乘以B中的 2 3
A中的3分别乘以B中的 3 4
A中的4分别乘以B中的 4 5
直接A.*B肯定不行
求大侠指点一下,怎么解决这个问题
看以用这样一个循环试试:
A=[1 2 3 4]; B=[1 2; 2 3; 3 4;4 5];
for i=1:4
for j=1:2
AB(i,j)=A(i)*B(i,j);
end
end
AB
下程序可否:
A=[1 2 3 4];
B=[1 2;2 3;3 4;4 5];
for i=1:4
C(i,: )=A(i).*B(i,: );
end
希望有帮助!!
laosam280
2009-04-30, 09:57
there is a more easier way to reach. you can change the dimensions of matrix A by using A,and than do a point time.
codes as follow are tested ok:
A=[1 2 3 4];
A1=[A;A]';
B=[1 2;2 3;3 4;4 5];
ans=A1.*B
>>
ans =
1 2
4 6
9 12
16 20
vBulletin® v3.8.3,版权所有 ©2000-2025,Jelsoft Enterprises Ltd.