gpt4 book ai didi

matlab - 将一个向量的每个元素乘以另一个向量的每个元素

转载 作者:行者123 更新时间:2023-12-02 14:56:25 25 4
gpt4 key购买 nike

我有两个非常大的列向量,AB , 大小 ax1bx1 , 分别。我想构造一个向量 C尺寸(b*a)x1通过计算 A(i)*B(j)对于每个 ij .为了说明我的意思:

clear
a=10^3;
b=10^3;
A=randn(a,1);
B=randn(b,1);
Ctemp=zeros(a,b);
for i=1:a
for j=1:b
Ctemp(i,j)=A(i)*B(j);
end
end
C=reshape(Ctemp, a*b,1);

问题:有没有更高效的方式获取C哪个避免双重循环?我的实际ab大于10^3 .

最佳答案

这是一个可以从隐式(或显式)扩展中获益的简单数组乘法示例:

% Implicit (R2016b and newer):
C = A(:) .* B(:).'; % optionally surround by reshape( ... , [], 1);

% "Explicit" (R2007a and newer):
C = bsxfun( @times, A(:), B(:).' );

从那里开始,这只是 reshape 的问题,正如您已经在做的那样(D = C(:)D = C(:).')。

关于matlab - 将一个向量的每个元素乘以另一个向量的每个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com