Create a 3D matrix from multiple 1D vectors without FOR loop

Hello everyone,
I would like to construct a three-dimensional 3x3xL matrix from a series of one-dimensional vectors.
So far I achieve this with the following code:
St = zeros(3.0, 3.0, L);
for i = 1:L
St(:, :, i) = [Sxxi(i), Txyi(i), Txzi(i);...
Txyi(i), Syyi(i), Tyzi(i);...
Txzi(i), Tyzi(i), Szzi(i)];
end
Where each of the six tensor components is a vector of size 1xL. I find this code to be quite silly and unnecessarily slow.
I'm wondering if there's a way to concatenate the six components into the 3x3xL matrix without using a FOR loop? At fist glance it looks like this might be achievable with CAT() but I'm not sure how this would work.
The parameter L is arbitrary so I'm trying to develop a solution which works for any length.
Thanks in advance,
Louis Vallance

4 件のコメント

KSSV
KSSV 2017 年 9 月 27 日
How many 1D vectors are there? What is size of each?
fsgeek
fsgeek 2017 年 9 月 27 日
There are six 1D vectors:
Sxx, Syy, Szz, Txy, Txz, Tyz
As stated above:
"... each of the six tensor components is a vector of size 1xL."
L is arbitrary so they can be any length, hence my problem. If knew the length in advance then I would probably just assign each slice in the 3D matrix individually.
Many thanks,
Louis
KSSV
KSSV 2017 年 9 月 27 日
But the loop you showed, will give you a 2D matrix..not 3D.
fsgeek
fsgeek 2017 年 9 月 27 日
I have corrected my original question to reflect the correct behaviour!
Thanks,
Louis

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2017 年 9 月 27 日
編集済み: Matt J 2017 年 9 月 27 日

1 投票

St=reshape( [Sxxi;Txyi;Txzi; Txyi;Syyi;Tyzi; Txzi;Tyzi;Syyi] , 3,3,[])

2 件のコメント

fsgeek
fsgeek 2017 年 9 月 27 日
This method works perfectly. I feel embarrassed that I didn't see it earlier!
Thanks,
Louis
Javier Fernandez
Javier Fernandez 2020 年 12 月 9 日
Sorry for reviving old threads, but I would like to point out that the given solution
St=reshape( [Sxxi;Txyi;Txzi; Txyi;Syyi;Tyzi; Txzi;Tyzi;Syyi] , 3,3,[])
is not fully correct because it actually gives the transpose of the expected result. In this case it makes no difference because the input tensor is symmetric and thus, it actually works.
The correct solution for non-symmetric matrices would need to account for that transpose by using permute as:
St=permute( reshape( [Sxxi;Txyi;Txzi; Txyi;Syyi;Tyzi; Txzi;Tyzi;Syyi] , 3,3,[]) , [2,1,3])
Javier

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2017 年 9 月 27 日
編集済み: Stephen23 2017 年 9 月 27 日

1 投票

St = zeros(3,3,L);
St(1,1,:) = Sxxi;
St(1,2,:) = Txyi;
St(1,3,:) = Txzi;
St(2,1,:) = Txyi;
St(2,2,:) = Syyi;
St(2,3,:) = Tyzi;
St(3,1,:) = Txzi;
St(3,2,:) = Tyzi;
St(3,3,:) = Szzi;

カテゴリ

ヘルプ センター および File ExchangeStress and Strain についてさらに検索

質問済み:

2017 年 9 月 27 日

コメント済み:

2020 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by