你可以使用3D矩阵来做到这一点:
[A,B] = meshgrid(0:10,1:10); U(1,1,:) = [1,1,0]; V(1,1,:) = [1,0,1]; s = A.*U + B.*V; % s is now a NxMx3 matrix, where N = length(A) and M = length(B) % We can plot how s varies with a and b as follows surf(A,B,s(:,:,1)); % first component surf(A,B,s(:,:,2)); % second component surf(A,B,s(:,:,3)); % third component
你想使用逐元素乘法( .* )因为你还想要治疗 a 和 b 作为标量(即单独使用每个元素)。
.*
a
b
您可以制作3D输出,其中每个2D切片对应于您的 meshgrid 输出,每个组件一个切片 U 和 V 。因此在这个例子中得到一个 10*11*3 矩阵。
meshgrid
U
V
10*11*3
要做到这一点,只需 reshape 该 U 和 V 矢量 1*1*3 在尺寸方面
reshape
1*1*3
U = reshape( [1,1,0], 1, 1, [] ); % Or equivalently U(1,1,:) = [1,1,0] V = reshape( [1,0,1], 1, 1, [] ); % Or equivalently U(1,1,:) = [1,0,1]
然后做元素乘法
s = a.*U + b.*V;
注意:在MATLAB R2016b之前(当引入隐式扩展时),您将不得不使用 bsxfun 获得等价物:
bsxfun
s = bsxfun( @times, a, U ) + bsxfun( @times, b, V );
然后你可以绘制 i 的元素 S 改变 A 和 B 通过绘图 s(:,:,i) 。
i
S
A
B
s(:,:,i)