向量数组相乘

时间:2022-01-26 11:58:15

I have three time series arrays a, b, c consisting of 1000 values each. They make up a matrix A.

我有3个时间序列a, b, c包含1000个值。它们构成一个矩阵a。

Now, I want to take each array and pointwise multiply it to every other array so that I will get 9 new vectors a^2, ab, ac, ba, b^2, bc, ca, cb, cc.

现在,我想把每个数组点态乘以其他数组,这样我会9新向量a ^ 2,ab,ac,英航,b ^ 2,公元前,ca,cb,cc。

When I've done this, I want to combine these 9 new arrays into 81 new.

当我这样做的时候,我想把这9个新数组合并成81个新数组。

How do I do this? Like I said, I tried building a matrix but it doesnt work the way I wanted it to. I want A to be recognized as a 1*3-matrix contaning 1000*1-arrays. As it is now it just concatenates everything. If A was a 1*3 matrix containing arrays, I could just build the matrix B = transpose(A) * A which would contain all products

我该怎么做呢?就像我说的,我试着建立一个矩阵,但它不是我想要的那样。我想让A被识别为1*3矩阵,包含1000*1个数组。因为它现在只是连接了所有东西。如果A是一个包含数组的1*3矩阵,我可以构建矩阵B =转置(A) * A,它包含所有的乘积。

I've tried things like

我试着像

A = [[a] [b] [c]]

A = {a b c}

A = {a; b; c}
defining a, b and c as a = {1, 2, 5, 2 , 1 ...} instead of [1, 2, 5, 2 , 1 ...]

but none of them works.

但它们都不起作用。

I don't care if a, b, c, d is stored as lists, column arrays, row arrays or cells, and I'm really not good enough at matlab to know the all the subtile differences, but speed and memory performance is sort of an issue.

我不关心ab c d是作为列表,列数组,行数组还是单元格来存储的,我真的不太擅长用matlab来了解所有的子块差异,但是速度和内存性能是一个问题。

2 个解决方案

#1


3  

This problem reduces down to creating index pairs.

这个问题可以归结为创建索引对。

%Simulate random data
X=rand(1000,3); 

%Create index multiplication pairs
[i,j] = meshgrid(1:size(X,2),1:size(X,2))

%Multiply together
X = X(:,i(:)).*X(:,j(:));

In this case you will get the following pairs of columns multiplied together

在这种情况下,您将得到以下对列相乘

[i(:)';j(:)'] = 
1     1     1     2     2     2     3     3     3
1     2     3     1     2     3     1     2     3

You can repeat the process to remultiply. Be careful though: the matrix size will grow exponentially in the number of iterations.

你可以重复这个过程来重做。但是要小心:矩阵的大小会随着迭代次数的增加而呈指数级增长。

#2


3  

You can do it as follows. Since the functions bsxfun and permute may not be obvious for a Matlab beginner, I suggest you take a look at their doc if needed (see links above).

你可以这样做。由于bsxfun和permute功能对于Matlab初学者来说可能不是很明显,所以我建议您在需要的时候查看一下它们的doc(请参阅上面的链接)。

Given the three data vectors a, b, c, proceed as follows:

给定三个数据向量a、b、c,进行如下操作:

A = [ a(:) b(:) c(:) ]; % matrix from column vectors
P = bsxfun(@times,A,permute(A,[1 3 2])); % desired result

The result P is a 1000x3x3 array that contains the desired products. The second and third indices of P are interpreted in the obvious way: 1 corresponds to a, 2 to b and 3 to c. For example, P(10,1,2) is a(10)*b(10); P(50,3,3) is c(50)^2; and so on.

结果P是一个1000x3x3的数组,包含所需的产品。P的第二和第三个指标以明显的方式解释:1对应于a, 2到b, 3到c。例如,P(10,1,2)是a(10)*b(10);P(50、3、3)是c(50)^ 2;等等。

To iterate: simply reshape P into a new A2 matrix and repeat procedure:

迭代:简单地将P重组为新的A2矩阵并重复步骤:

A2 = reshape(P,[1000,9,1]);
P2 = bsxfun(@times,A2,permute(A2,[1 3 2])); % result

This gives the result in the 1000x9x9 array P2.

这将得到1000x9x9数组P2的结果。

#1


3  

This problem reduces down to creating index pairs.

这个问题可以归结为创建索引对。

%Simulate random data
X=rand(1000,3); 

%Create index multiplication pairs
[i,j] = meshgrid(1:size(X,2),1:size(X,2))

%Multiply together
X = X(:,i(:)).*X(:,j(:));

In this case you will get the following pairs of columns multiplied together

在这种情况下,您将得到以下对列相乘

[i(:)';j(:)'] = 
1     1     1     2     2     2     3     3     3
1     2     3     1     2     3     1     2     3

You can repeat the process to remultiply. Be careful though: the matrix size will grow exponentially in the number of iterations.

你可以重复这个过程来重做。但是要小心:矩阵的大小会随着迭代次数的增加而呈指数级增长。

#2


3  

You can do it as follows. Since the functions bsxfun and permute may not be obvious for a Matlab beginner, I suggest you take a look at their doc if needed (see links above).

你可以这样做。由于bsxfun和permute功能对于Matlab初学者来说可能不是很明显,所以我建议您在需要的时候查看一下它们的doc(请参阅上面的链接)。

Given the three data vectors a, b, c, proceed as follows:

给定三个数据向量a、b、c,进行如下操作:

A = [ a(:) b(:) c(:) ]; % matrix from column vectors
P = bsxfun(@times,A,permute(A,[1 3 2])); % desired result

The result P is a 1000x3x3 array that contains the desired products. The second and third indices of P are interpreted in the obvious way: 1 corresponds to a, 2 to b and 3 to c. For example, P(10,1,2) is a(10)*b(10); P(50,3,3) is c(50)^2; and so on.

结果P是一个1000x3x3的数组,包含所需的产品。P的第二和第三个指标以明显的方式解释:1对应于a, 2到b, 3到c。例如,P(10,1,2)是a(10)*b(10);P(50、3、3)是c(50)^ 2;等等。

To iterate: simply reshape P into a new A2 matrix and repeat procedure:

迭代:简单地将P重组为新的A2矩阵并重复步骤:

A2 = reshape(P,[1000,9,1]);
P2 = bsxfun(@times,A2,permute(A2,[1 3 2])); % result

This gives the result in the 1000x9x9 array P2.

这将得到1000x9x9数组P2的结果。