Taking outer product of two matrices

I have a 3x3 displacement matrix (let us call it u). The displacement gradient tensor F is given by
F = I + ∇ ⊗ u
where,
I = identity matrix
∇ = gradient operator
⊗ = outer product of two matrices,
Can someone help me code this in MATLAB?

11 commentaires

Rik
Rik le 13 Juil 2024
What did you try? Did you Google this?
Jatin
Jatin le 15 Juil 2024
Hi, this can be done with a basic code in MATLAB, to help you get started with MATLAB here is the documentation : Get Started with MATLAB (mathworks.com)
Umar
Umar le 15 Juil 2024

Hi Priyanshu,

In order to help you out with your code, I will first generate a random 3x3 displacement matrix u using the rand function.Then, define the identity matrix I using the eye function and a simple gradient operator grad_op for demonstration purposes. Afterwards, I will use displacement gradient tensor F which is calculated using the formula F = I + grad_op * u. Finally, display the calculated displacement gradient tensor F using the disp function. Please see attached results.

Priyanshu
Priyanshu le 15 Juil 2024
Hi @Umar,
Thank you for your solution. However, the ⊗ operator between ∇ and u isn't the simple multiplication operator *. It's the outer product operator and hence I am finding it difficult to code it in MATLAB. Here's more about outer product
Priyanshu
Priyanshu le 15 Juil 2024
I didn't find any help in the MATLAB documentation for outer product. Can you be more specific?
Umar
Umar le 15 Juil 2024
Modifié(e) : Walter Roberson le 15 Juil 2024
Hi Priyanshu,
The outer product operation between ∇ and u can be achieved using element-wise multiplication along the third dimension. For more information on element wise multiplication, please refer to https://www.mathworks.com/support/search.html/answers/174970-element-wise-multiplication-beginner.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:support/matrix-in380&page=1
Here is an example snippet code to achieve this:
outer_product = zeros(3,3,3);
for i = 1:3
for j = 1:3
outer_poduct(:,:,i) = outer_product(:,:,i) + grad_op(:,:,j) * u(j,i);
end
end
This should help resolve your problem now. Please let me know if you have any further questions.
Stephen23
Stephen23 le 15 Juil 2024
Modifié(e) : Stephen23 le 15 Juil 2024
@Umar: that is true for vectors, which is what most beginners learn about. However the outer product is also defined for arrays (i.e. tensors) of arbitrary dimensions:
Applying a simple matrix multiplication to such tensors would either throw an error or give an incorrect output.
Priyanshu
Priyanshu le 15 Juil 2024
@Stephen23, I agree with your point. And this is what I am struggling with. Element-wise multiplication is easy but it gives us a Kronecker multiplication and not an outer product of tensors.
Umar
Umar le 15 Juil 2024
Modifié(e) : Umar le 15 Juil 2024
Hi Stephen23,
It was missing details such as outer product of tensors. Hence, illustrated my example with element wise multiplication.
Stephen23
Stephen23 le 15 Juil 2024
Modifié(e) : Stephen23 le 15 Juil 2024
"Hence, illustrated my example with element wise multiplication."
None of your code uses element-wise multiplication.
Umar
Umar le 15 Juil 2024
Hi @Stephen23,
I never said that my code uses element wise application. To help you understand, it is basically very simple to understand, it is attempting to calculate the outer product of two vectors which results in a matrix where the (i,j)th entry is given by the product of the ith element of u and the jth element of v. For more information regarding basic concepts of array and matrixes, please refer to https://www.mathworks.com/help/matlab/learn_matlab/matrices-and-arrays.html Again, thanks for your contribution and feedback.

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 15 Juil 2024
Modifié(e) : Stephen23 le 15 Juil 2024
"However, the ⊗ operator between ∇ and u isn't the simple multiplication operator *."
The Wikipedia page you linked to states "The outer product 𝑢⊗𝑣 is equivalent to a matrix multiplication 𝑢𝑣T, provided that 𝑢 is represented as a 𝑚×1 column vector and 𝑣 as a 𝑛×1 column vector (which makes 𝑣T a row vector)." So for vectors you can certainly use matrix multiplication. For higher dimension arrays you could leverage e.g. RESHAPE and TIMES ... or read the next part of my answer.
"It's the outer produt operator and hence I am finding it difficult to code it in MATLAB"
Google found this in two seconds (note: >=R2022a only):
A = rand(3,3);
B = rand(3,3);
C = tensorprod(A,B)
C =
C(:,:,1,1) = 0.0016 0.0022 0.0001 0.0014 0.0007 0.0031 0.0041 0.0036 0.0037 C(:,:,2,1) = 0.0300 0.0403 0.0015 0.0265 0.0136 0.0581 0.0759 0.0680 0.0701 C(:,:,3,1) = 0.1153 0.1550 0.0059 0.1018 0.0525 0.2235 0.2921 0.2617 0.2698 C(:,:,1,2) = 0.1815 0.2438 0.0093 0.1602 0.0826 0.3515 0.4596 0.4117 0.4244 C(:,:,2,2) = 0.0327 0.0440 0.0017 0.0289 0.0149 0.0634 0.0829 0.0743 0.0766 C(:,:,3,2) = 0.1576 0.2118 0.0081 0.1392 0.0717 0.3054 0.3992 0.3576 0.3687 C(:,:,1,3) = 0.1604 0.2154 0.0082 0.1416 0.0730 0.3107 0.4061 0.3638 0.3751 C(:,:,2,3) = 0.2397 0.3220 0.0123 0.2116 0.1091 0.4643 0.6070 0.5437 0.5606 C(:,:,3,3) = 0.1568 0.2107 0.0081 0.1385 0.0714 0.3038 0.3972 0.3558 0.3669

4 commentaires

Umar
Umar le 15 Juil 2024
Thanks for your help @Stephen23.
Hi Priyanshu, does this resolve your issue. We both did our best to resolve your problem. Hope with this provided guidance, you can proceed to solve your problem. Please also give a vote to @Stephen23.
Stephen23
Stephen23 le 15 Juil 2024
Modifié(e) : Stephen23 le 15 Juil 2024
For earlier versions here is a general solution which will work for arrays of arbitrary dimensions, using the example here:
A = rand(3,5,7); A(2,2,4) = 11;
B = rand(10,100); B(8,88) = 13;
N = nnz(cumsum(fliplr(size(A))-1)); % NDIMS, with special handling for vectors
C = A.*reshape(B,[ones(1,N),size(B)]); % replace TIMES with BSXFUN if required
size(C)
ans = 1x5
3 5 7 10 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C(2,2,4,8,88) % 143
ans = 143
Checking against the inbuilt function:
D = tensorprod(A,B);
isequal(C,D)
ans = logical
1
Priyanshu
Priyanshu le 16 Juil 2024
Thank you for helping me with the concept. I understand the logic and i think the code will be good for me.
Umar
Umar le 16 Juil 2024
No problem Priyanshu, glad to help you out. Please let us know if you still have any further questions, all our staff people are very knowledgeable and happy to help out.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by