A matrix times a vector whose elements are matrix too.

3 vues (au cours des 30 derniers jours)
Wang
Wang le 27 Avr 2023
Commenté : Wang le 28 Avr 2023
Here, we use a matrix Sx times a vector initialvector whose elements are also matrix, the code is in the following. And we expect Sx*initialvector could give a matrix with size [5*5; 5*5; 5*5], similar with initialvector. What should we do?
Sx=1/sqrt(2)*[0 1 0; 1 0 1; 0 1 0];
initialu1=rand(5);
initialu0=rand(5);
initialuminus1=rand(5);
initialvector=[initialu1; initialu0; initialuminus1;];
Sx*initialvector
Added: Take a very simple example, if the elements initialu1, initialu0 and initialuminus1 are just scalar numbers, we calculate
ones(3)*initialvector
which should give
[initialu1+initialu0+initialuminus1; initialu1+initialu0+initialuminus1; initialu1+initialu0+initialuminus1]
However, initialu1, initialu0 and initialuminus1 are matrix here. But we still want the reslut
[initialu1+initialu0+initialuminus1; initialu1+initialu0+initialuminus1; initialu1+initialu0+initialuminus1]
which is a matrix now, and whose size is same with initialvector...
  4 commentaires
Steven Lord
Steven Lord le 27 Avr 2023
How do you get from:
% ones(3)*initialvector
to
% initialu1+initialu0+initialuminus1
Let's take a concrete example of an initial vector that contains scalars.
initialvector = [1; 2; 4];
result = ones(3)*initialvector
result = 3×1
7 7 7
The result is a 3-by-1 vector, the same size as initialvector. Each element of result is the sum of the elements of initialvector, but how are you collapsing that vector to the scalar value resulting from summing the elements of initialvector?
And if we had a different matrix:
result2 = [0 1 0; 1 0 1; 0 1 0]*initialvector
result2 = 3×1
2 5 2
How specifically would you collapse that vector into a scalar? Is the expected result 3, 5, 2, or something else entirely?
mean(result2)
ans = 3
max(result2)
ans = 5
min(result2)
ans = 2
result2(1)
ans = 2
Wang
Wang le 28 Avr 2023
@Steven Lord Sorry about that. I made a mistake. And the question is updated.
What I want to say is that there is no collapse. For example, if initialvector is a matrix,like
initialvector=[ones(5); ones(5); ones(5);];
The desired answer of ones(3)*initialvector should be
[3*ones(5); 3*ones(5); 3*ones(5);]

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 27 Avr 2023
a vector initialvector whose elements are also matrix
That doesn't exist in MATLAB, at least not if you want initialvector to be a numeric vector. Your initialvector isn't a vector, it's a matrix.
You could make a cell array that is a vector and have each cell in the cell array contain a matrix, but multiplication is not defined for cell arrays.
c = {magic(5), ones(5), rand(5)}
c = 1×3 cell array
{5×5 double} {5×5 double} {5×5 double}
isvector(c)
ans = logical
1
Even if you used c and indexed into it to extract the matrices stored in each cell, matrix multiplication between a 3-by-3 matrix and a 5-by-5 matrix is not mathematically defined. But from the structure of that Sx matrix I wonder if maybe you're trying to do some type of convolution or filtering. If so conv2 or filter2 might be what you're looking for.
Sx=1/sqrt(2)*[0 1 0; 1 0 1; 0 1 0];
M = magic(5);
conv2(M, Sx, 'same')
ans = 5×5
33.2340 16.2635 27.5772 21.2132 16.9706 18.3848 42.4264 23.3345 36.0624 36.0624 27.5772 24.0416 36.7696 49.4975 27.5772 19.0919 37.4767 50.2046 31.1127 36.7696 19.7990 33.9411 27.5772 38.8909 3.5355
  1 commentaire
Wang
Wang le 27 Avr 2023
Thanks for your patience @Steven Lord! Some explanations are added in the question. Hope it's clear.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by