📄 c_ttensor_doc.html
字号:
0.2709 0.1444 0.3425 0.1631ans(:,:,2) = 0.4974 0.2569 0.6430 0.3142 0.5173 0.2573 0.6857 0.3445 0.3132 0.1414 0.4398 0.2343 0.4318 0.2359 0.5363 0.2498 0.3181 0.1696 0.4022 0.1915ans(:,:,3) = 1.1514 0.5948 1.4883 0.7272 1.1975 0.5956 1.5872 0.7974 0.7251 0.3273 1.0180 0.5423 0.9996 0.5461 1.2413 0.5783 0.7363 0.3925 0.9310 0.4434</pre><h2>Use ndims and size to get the size of a ttensor<a name="14"></a></h2><pre class="codeinput">ndims(X) <span class="comment">%<-- Number of dimensions.</span></pre><pre class="codeoutput">ans = 3</pre><pre class="codeinput">size(X) <span class="comment">%<-- Row vector of the sizes.</span></pre><pre class="codeoutput">ans = 5 4 3</pre><pre class="codeinput">size(X,2) <span class="comment">%<-- Size of the 2nd mode.</span></pre><pre class="codeoutput">ans = 4</pre><h2>Subscripted reference to a ttensor<a name="17"></a></h2><pre class="codeinput">X.core(1,1,1) <span class="comment">%<-- Access an element of the core.</span></pre><pre class="codeoutput">ans = 0.0142</pre><pre class="codeinput">X.U{2} <span class="comment">%<-- Extract a matrix.</span></pre><pre class="codeoutput">ans = 0.9408 0.4551 0.7019 0.0811 0.8477 0.8511 0.2093 0.5620</pre><pre class="codeinput">X{2} <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">ans = 0.9408 0.4551 0.7019 0.0811 0.8477 0.8511 0.2093 0.5620</pre><h2>Subscripted assignment for a ttensor<a name="20"></a></h2><pre class="codeinput">X.core = tenones(size(X.core)) <span class="comment">%<-- Insert a new core.</span></pre><pre class="codeoutput">X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 1 1 1 X.U{1} = 0.5221 0.1722 0.8948 0.9329 0.9688 0.2861 0.7134 0.3557 0.2512 0.2280 0.0490 0.9327 0.4496 0.7553 0.1310 X.U{2} = 0.9408 0.4551 0.7019 0.0811 0.8477 0.8511 0.2093 0.5620 X.U{3} = 0.3193 0.3749 0.8678</pre><pre class="codeinput">X.core(2,2,1) = 7 <span class="comment">%<-- Change a single element.</span></pre><pre class="codeoutput">X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 7 1 1 X.U{1} = 0.5221 0.1722 0.8948 0.9329 0.9688 0.2861 0.7134 0.3557 0.2512 0.2280 0.0490 0.9327 0.4496 0.7553 0.1310 X.U{2} = 0.9408 0.4551 0.7019 0.0811 0.8477 0.8511 0.2093 0.5620 X.U{3} = 0.3193 0.3749 0.8678</pre><pre class="codeinput">X{3}(1:2,1) = [1;1] <span class="comment">%<-- Change the matrix for mode 3.</span></pre><pre class="codeoutput">X is a ttensor of size 5 x 4 x 3 X.core is a tensor of size 3 x 2 x 1 X.core(:,:,1) = 1 1 1 7 1 1 X.U{1} = 0.5221 0.1722 0.8948 0.9329 0.9688 0.2861 0.7134 0.3557 0.2512 0.2280 0.0490 0.9327 0.4496 0.7553 0.1310 X.U{2} = 0.9408 0.4551 0.7019 0.0811 0.8477 0.8511 0.2093 0.5620 X.U{3} = 1.0000 1.0000 0.8678</pre><h2>Using end for last index<a name="23"></a></h2><pre class="codeinput">X{end} <span class="comment">%<-- The same as X{3}.</span></pre><pre class="codeoutput">ans = 1.0000 1.0000 0.8678</pre><h2>Basic operations (uplus, uminus, mtimes) for a ttensor.<a name="24"></a></h2><pre class="codeinput">X = ttensor(tenrand([2 2 2]),{rand(3,2),rand(1,2),rand(2,2)}) <span class="comment">%<-- Data.</span>+X <span class="comment">%<-- Calls uplus.</span></pre><pre class="codeoutput">X is a ttensor of size 3 x 1 x 2 X.core is a tensor of size 2 x 2 x 2 X.core(:,:,1) = 0.8167 0.0174 0.9855 0.8194 X.core(:,:,2) = 0.6211 0.2440 0.5602 0.8220 X.U{1} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049 X.U{2} = 0.6595 0.1834 X.U{3} = 0.6365 0.5396 0.1703 0.6234ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.8167 0.0174 0.9855 0.8194 ans.core(:,:,2) = 0.6211 0.2440 0.5602 0.8220 ans.U{1} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049 ans.U{2} = 0.6595 0.1834 ans.U{3} = 0.6365 0.5396 0.1703 0.6234</pre><pre class="codeinput">-X <span class="comment">%<-- Calls uminus.</span></pre><pre class="codeoutput">ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = -0.8167 -0.0174 -0.9855 -0.8194 ans.core(:,:,2) = -0.6211 -0.2440 -0.5602 -0.8220 ans.U{1} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049 ans.U{2} = 0.6595 0.1834 ans.U{3} = 0.6365 0.5396 0.1703 0.6234</pre><pre class="codeinput">5*X <span class="comment">%<-- Calls mtimes.</span></pre><pre class="codeoutput">ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 4.0837 0.0868 4.9274 4.0970 ans.core(:,:,2) = 3.1057 1.2202 2.8011 4.1100 ans.U{1} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049 ans.U{2} = 0.6595 0.1834 ans.U{3} = 0.6365 0.5396 0.1703 0.6234</pre><h2>Use permute to reorder the modes of a ttensor<a name="27"></a></h2><pre class="codeinput">permute(X,[3 2 1]) <span class="comment">%<-- Reverses the modes of X</span></pre><pre class="codeoutput">ans is a ttensor of size 2 x 1 x 3 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.8167 0.0174 0.6211 0.2440 ans.core(:,:,2) = 0.9855 0.8194 0.5602 0.8220 ans.U{1} = 0.6365 0.5396 0.1703 0.6234 ans.U{2} = 0.6595 0.1834 ans.U{3} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049</pre><h2>Displaying a ttensor<a name="28"></a></h2> <p>The tensor displays by displaying the core and each of the component matrices.</p><pre class="codeinput">disp(X) <span class="comment">%<-- Prints out the ttensor.</span></pre><pre class="codeoutput">ans is a ttensor of size 3 x 1 x 2 ans.core is a tensor of size 2 x 2 x 2 ans.core(:,:,1) = 0.8167 0.0174 0.9855 0.8194 ans.core(:,:,2) = 0.6211 0.2440 0.5602 0.8220 ans.U{1} = 0.2632 0.2141 0.7536 0.6021 0.6596 0.6049 ans.U{2} = 0.6595 0.1834 ans.U{3} = 0.6365 0.5396 0.1703 0.6234</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Tucker Tensors
% Tucker format is a decomposition of a tensor X as the product of a core
% tensor G and matrices (e.g., A,B,C) in each dimension. In other words, a
% tensor X is expressed as:
%
% $${\mathcal X} = {\mathcal G} \times_1 A \times_2 B \times_2 C$$
%
% In MATLAB notation, |X=ttm(G,{A,B,C})|. The |ttensor| class stores the
% components of the tensor X and can perform many operations, e.g., |ttm|,
% without explicitly forming the tensor X.
%% Creating a ttensor with a tensor core
core = tensor(rand(3,2,1),[3 2 1]); %<REPLACE_WITH_DASH_DASH The core tensor.
U = {rand(5,3), rand(4,2), rand(3,1)}; %<REPLACE_WITH_DASH_DASH The matrices.
X = ttensor(core,U) %<REPLACE_WITH_DASH_DASH Create the ttensor.
%% Alternate core formats: sptensor, ktensor, or ttensor
core1 = sptenrand([3 2 1],3); %<REPLACE_WITH_DASH_DASH Create a 3 x 2 x 1 sptensor.
Y = ttensor(core1,U) %<REPLACE_WITH_DASH_DASH Core is a sptensor.
%%
V = {rand(3,2),rand(2,2),rand(1,2)}; %<REPLACE_WITH_DASH_DASH Create some random matrices.
core2 = ktensor(V); %<REPLACE_WITH_DASH_DASH Create a 3 x 2 x 1 ktensor.
Y = ttensor(core2,U) %<REPLACE_WITH_DASH_DASH Core is a ktensor.
%%
core3 = ttensor(tensor(1:8,[2 2 2]),V); %<REPLACE_WITH_DASH_DASH Create a 3 x 2 x 1 ttensor.
Y = ttensor(core3,U) %<REPLACE_WITH_DASH_DASH Core is a ttensor.
%% Creating a one-dimensional ttensor
Z = ttensor(tensor(rand(2,1),2), rand(4,2)) %<REPLACE_WITH_DASH_DASH One-dimensional ttensor.
%% Constituent parts of a ttensor
X.core %<REPLACE_WITH_DASH_DASH Core tensor.
%%
X.U %<REPLACE_WITH_DASH_DASH Cell array of matrices.
%% Creating a ttensor from its constituent parts
Y = ttensor(X.core,X.U) %<REPLACE_WITH_DASH_DASH Recreate a tensor from its parts.
%% Creating an empty ttensor.
X = ttensor %<REPLACE_WITH_DASH_DASH empty ttensor
%% Use full or tensor to convert a ttensor to a tensor
X = ttensor(core,U) %<REPLACE_WITH_DASH_DASH Create a tensor
%%
full(X) %<REPLACE_WITH_DASH_DASH Converts to a tensor.
%%
tensor(X) %<REPLACE_WITH_DASH_DASH Also converts to a tensor.
%% Use double to convert a ttensor to a (multidimensional) array
double(X) %<REPLACE_WITH_DASH_DASH Converts to a MATLAB array
%% Use ndims and size to get the size of a ttensor
ndims(X) %<REPLACE_WITH_DASH_DASH Number of dimensions.
%%
size(X) %<REPLACE_WITH_DASH_DASH Row vector of the sizes.
%%
size(X,2) %<REPLACE_WITH_DASH_DASH Size of the 2nd mode.
%% Subscripted reference to a ttensor
X.core(1,1,1) %<REPLACE_WITH_DASH_DASH Access an element of the core.
%%
X.U{2} %<REPLACE_WITH_DASH_DASH Extract a matrix.
%%
X{2} %<REPLACE_WITH_DASH_DASH Same as above.
%% Subscripted assignment for a ttensor
X.core = tenones(size(X.core)) %<REPLACE_WITH_DASH_DASH Insert a new core.
%%
X.core(2,2,1) = 7 %<REPLACE_WITH_DASH_DASH Change a single element.
%%
X{3}(1:2,1) = [1;1] %<REPLACE_WITH_DASH_DASH Change the matrix for mode 3.
%% Using end for last index
X{end} %<REPLACE_WITH_DASH_DASH The same as X{3}.
%% Basic operations (uplus, uminus, mtimes) for a ttensor.
X = ttensor(tenrand([2 2 2]),{rand(3,2),rand(1,2),rand(2,2)}) %<REPLACE_WITH_DASH_DASH Data.
+X %<REPLACE_WITH_DASH_DASH Calls uplus.
%%
-X %<REPLACE_WITH_DASH_DASH Calls uminus.
%%
5*X %<REPLACE_WITH_DASH_DASH Calls mtimes.
%% Use permute to reorder the modes of a ttensor
permute(X,[3 2 1]) %<REPLACE_WITH_DASH_DASH Reverses the modes of X
%% Displaying a ttensor
% The tensor displays by displaying the core and each of the component
% matrices.
disp(X) %<REPLACE_WITH_DASH_DASH Prints out the ttensor.
##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -