MATLAB 中的向量到矩阵语法

发布于 2024-07-25 20:16:20 字数 332 浏览 2 评论 0 原文

有没有办法在 MATLAB 中组合 2 个向量,以便:

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

使用类似于以下的正常 MATLAB 语法:

mat = C * S(1:length(S))

这会给出“内部矩阵维度必须一致错误”,因为它试图执行正常的矩阵运算。 这不是标准的线性代数运算,所以我不确定如何在 MATLAB 中正确表达它,但似乎不需要循环就可以实现,而循环在 MATLAB 中太慢了。

Is there a way to combine 2 vectors in MATLAB such that:

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

Using normal MATLAB syntax similar to:

mat = C * S(1:length(S))

This gives a "Inner matrix dimensions must agree error" because it's trying to do normal matrix operations. This is not a standard Linear Algebra operation so I'm not sure how to correctly express it in MATLAB, but it seems like it should be possible without requiring a loop, which is excessively slow in MATLAB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街角卖回忆 2024-08-01 20:16:20

从你的描述来看,这听起来像是一个简单的矩阵运算。 您只需确保 CS 的尺寸正确即可。 C 应该是列向量 (length(C)-by-1),S 应该是行向量 (1-by-<强>长度(S))。 假设它们的尺寸正确,只需执行以下操作:

mat = C*S;

如果您不确定它们的尺寸,这应该可行:

mat = (C(:))*(S(:)');

编辑:实际上,我对括号有点疯狂。 其中一些是不必要的,因为不存在操作顺序问题。 这是一个更清晰的版本:

mat = C(:)*S(:)';

说明:

矩阵乘法运算符将产生内积(结果标量值)或外积(生成矩阵),具体取决于它所应用到的向量的维度。

由于使用了 ,上面的最后一个方程产生了一个外积冒号运算符 来重塑向量参数的维度。 语法 C(:)C 的内容重塑为单个列向量。 语法 S(:)'S 的内容重塑为列向量,然后将其转置为行向量。 相乘时,会产生一个大小为(length(C)-by-length(S))的矩阵。

注意:冒号运算符的这种用法适用于任何维度的向量和矩阵,允许您将它们的内容重新整形为单个列向量(这使得某些操作更容易,如 这个其他问题)。

From your description, it sounds like a simple matrix operation. You just have to make sure you have the right dimensions for C and S. C should be a column vector (length(C)-by-1) and S should be a row vector (1-by-length(S)). Assuming they are the right dimensions, just do the following:

mat = C*S;

If you're not sure of their dimensions, this should work:

mat = (C(:))*(S(:)');

EDIT: Actually, I went a little crazy with the parentheses. Some of them are unnecessary, since there are no order-of-operation concerns. Here's a cleaner version:

mat = C(:)*S(:)';

EXPLANATION:

The matrix multiplication operator in MATLAB will produce either an inner product (resulting in a scalar value) or an outer product (resulting in a matrix) depending on the dimensions of the vectors it is applied to.

The last equation above produces an outer product because of the use of the colon operator to reshape the dimensions of the vector arguments. The syntax C(:) reshapes the contents of C into a single column vector. The syntax S(:)' reshapes the contents of S into a column vector, then transposes it into a row vector. When multiplied, this results in a matrix of size (length(C)-by-length(S)).

Note: This use of the colon operator is applicable to vectors and matrices of any dimension, allowing you to reshape their contents into a single column vector (which makes some operations easier, as shown by this other SO question).

巾帼英雄 2024-08-01 20:16:20

尝试在 MATLAB 中执行此操作:

mat = C*S'

如下所示:

C = [1; 2; 3];
S = [2; 2; 9; 1];

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

% Equivalent code:
mat2 = C*S';

myDiff = mat - mat2

Try executing this in MATLAB:

mat = C*S'

As In:

C = [1; 2; 3];
S = [2; 2; 9; 1];

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

% Equivalent code:
mat2 = C*S';

myDiff = mat - mat2
丑丑阿 2024-08-01 20:16:20

您的意思是以下内容吗?

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

如果是这样,那就只是矩阵乘法:

C' * S    % if C and S are row vectors
C * S'    % if C and S are column vectors

如果你不知道CS是行向量还是列向量,你可以使用一个技巧将它们变成列向量,然后在将它们相乘之前转置 S

C(:) * S(:)'

Do you mean the following?

mat = zeros(length(C),length(S));
for j=1:length(C)
    mat(j,:)=C(j)*S;
end

If so, it's simply matrix multiplication:

C' * S    % if C and S are row vectors
C * S'    % if C and S are column vectors

If you don't know whether C and S are row vectors or column vectors, you can use a trick to turn them into column vectors, then transpose S before multiplying them:

C(:) * S(:)'
妄司 2024-08-01 20:16:20

我不完全清楚你在做什么 - 看起来你的结果矩阵将由 length(C) 行组成,其中第 i 行是向量SC 的第 i 条目缩放(因为向量的下标给出了标量)。 在这种情况下,您可以执行类似

mat = repmat(C,[1 length(S)]) .* repmat(S, [length(C) 1])

在列之间平铺 C 以及在行之间平铺 S 的操作。

I'm not entirely clear on what you're doing - it looks like your resulting matrix will consist of length(C) rows, where the ith row is the vector S scaled by the ith entry of C (since subscripting a vector gives a scalar). In this case, you can do something like

mat = repmat(C,[1 length(S)]) .* repmat(S, [length(C) 1])

where you tile C across columns, and S down rows.

何必那么矫情 2024-08-01 20:16:20

试试这个:

C = 1:3
S = 1:5
mat1 = C'*S

mat2 = bsxfun(@times, C',S)

(特别是当您需要的函数不是更简单的 MATLAB 表示法时,这很好)

--Loren

Try this:

C = 1:3
S = 1:5
mat1 = C'*S

mat2 = bsxfun(@times, C',S)

(esp. good when the function you need isn't simpler MATLAB notation)

--Loren

疯了 2024-08-01 20:16:20

尝试使用网格网格:

[Cm, Sm] = meshgrid(C, S);
mat = Cm .* Sm;

编辑:没关系,矩阵乘法也可以。 你只需要一个列向量C和一个行向量S。然后做C * S。

Try using meshgrid:

[Cm, Sm] = meshgrid(C, S);
mat = Cm .* Sm;

edit: nevermind, matrix multiplication will do too. You just need one column vector C and one row vector S. Then do C * S.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文