📄 b1_tenmat_doc.html
字号:
1 4 3</pre><h2>Creating a tenmat from its constituent parts<a name="16"></a></h2><pre class="codeinput">B = tenmat(A.data,A.rdims,A.cdims,A.tsize) <span class="comment">%<-- Recreates A</span></pre><pre class="codeoutput">B is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 B.rindices = [ 2 ] (modes of tensor corresponding to rows) B.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) B.data = 1 2 3 13 14 15 7 8 9 19 20 21 4 5 6 16 17 18 10 11 12 22 23 24</pre><h2>Creating an empty tenmat<a name="17"></a></h2><pre class="codeinput">B = tenmat <span class="comment">%<-- Empty tenmat.</span></pre><pre class="codeoutput">B is a matrix corresponding to a tensor of size [empty tensor] B.rindices = [ ] (modes of tensor corresponding to rows) B.cindices = [ ] (modes of tensor corresponding to columns) B.data = []</pre><h2>Use double to convert a tenmat to a MATLAB matrix<a name="18"></a></h2><pre class="codeinput">double(A) <span class="comment">%<-- converts A to a standard matrix</span></pre><pre class="codeoutput">ans = 1 2 3 13 14 15 7 8 9 19 20 21 4 5 6 16 17 18 10 11 12 22 23 24</pre><h2>Use tensor to convert a tenmat to a tensor<a name="19"></a></h2><pre class="codeinput">Y = tensor(A)</pre><pre class="codeoutput">Y is a tensor of size 3 x 2 x 2 x 2 Y(:,:,1,1) = 1 4 2 5 3 6 Y(:,:,2,1) = 7 10 8 11 9 12 Y(:,:,1,2) = 13 16 14 17 15 18 Y(:,:,2,2) = 19 22 20 23 21 24</pre><h2>Use size and tsize for the dimensions of a tenmat<a name="20"></a></h2><pre class="codeinput">size(A) <span class="comment">%<-- Matrix size</span>tsize(A) <span class="comment">%<-- Corresponding tensor size</span></pre><pre class="codeoutput">ans = 2 12ans = 3 2 2 2</pre><h2>Subscripted reference for a tenmat<a name="21"></a></h2><pre class="codeinput">A(2,1) <span class="comment">%<-- returns the (2,1) element of the matrix.</span></pre><pre class="codeoutput">ans = 4</pre><h2>Subscripted assignment for a tenmat<a name="22"></a></h2><pre class="codeinput">A(1:2,1:2) = ones(2) <span class="comment">%<-- Replace part of the matrix.</span></pre><pre class="codeoutput">A is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 A.rindices = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) A.data = 1 1 3 13 14 15 7 8 9 19 20 21 1 1 6 16 17 18 10 11 12 22 23 24</pre><h2>Use end for the last index<a name="23"></a></h2><pre class="codeinput">A(end,end) <span class="comment">%<-- Same as X(2,12)</span></pre><pre class="codeoutput">ans = 24</pre><h2>Basic operations for tenmat<a name="24"></a></h2><pre class="codeinput">norm(A) <span class="comment">%<-- Norm of the matrix.</span></pre><pre class="codeoutput">ans = 69.6994</pre><pre class="codeinput">A' <span class="comment">%<-- Calls ctranspose (also swaps mapped dimensions).</span></pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 1 4 3 ] (modes of tensor corresponding to rows) ans.cindices = [ 2 ] (modes of tensor corresponding to columns) ans.data = 1 1 1 1 3 6 13 16 14 17 15 18 7 10 8 11 9 12 19 22 20 23 21 24</pre><pre class="codeinput">+A <span class="comment">%<-- Calls uplus.</span></pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 2 ] (modes of tensor corresponding to rows) ans.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) ans.data = 1 1 3 13 14 15 7 8 9 19 20 21 1 1 6 16 17 18 10 11 12 22 23 24</pre><pre class="codeinput">-A <span class="comment">%<-- Calls uminus.</span></pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 2 ] (modes of tensor corresponding to rows) ans.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) ans.data = -1 -1 -3 -13 -14 -15 -7 -8 -9 -19 -20 -21 -1 -1 -6 -16 -17 -18 -10 -11 -12 -22 -23 -24</pre><pre class="codeinput">A+A <span class="comment">%<-- Calls plus.</span></pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 2 ] (modes of tensor corresponding to rows) ans.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) ans.data = 2 2 6 26 28 30 14 16 18 38 40 42 2 2 12 32 34 36 20 22 24 44 46 48</pre><pre class="codeinput">A-A <span class="comment">%<-- Calls minus.</span></pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 2 ] (modes of tensor corresponding to rows) ans.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) ans.data = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</pre><h2>Multiplying two tenmats<a name="30"></a></h2> <p>It is possible to compute the product of two tenmats and have a result that can be converted into a tensor.</p><pre class="codeinput">B = A * A' <span class="comment">%<-- Tenmat that is the product of two tenmats.</span></pre><pre class="codeoutput">B is a matrix corresponding to a tensor of size 2 x 2 B.rindices = [ 1 ] (modes of tensor corresponding to rows) B.cindices = [ 2 ] (modes of tensor corresponding to columns) B.data = 1997 2384 2384 2861</pre><pre class="codeinput">tensor(B) <span class="comment">%<-- Corresponding tensor.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 2 ans(:,:) = 1997 2384 2384 2861</pre><h2>Displaying a tenmat<a name="32"></a></h2> <p>Shows the original tensor dimensions, the modes mapped to rows, the modes mapped to columns, and the matrix.</p><pre class="codeinput">disp(A)</pre><pre class="codeoutput">ans is a matrix corresponding to a tensor of size 3 x 2 x 2 x 2 ans.rindices = [ 2 ] (modes of tensor corresponding to rows) ans.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) ans.data = 1 1 3 13 14 15 7 8 9 19 20 21 1 1 6 16 17 18 10 11 12 22 23 24</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Converting a tensor to a matrix and vice versa
% We show how to convert a tensor to a matrix stored with extra information
% so that it can be converted back to a tensor. Converting to a matrix
% requies an ordered mapping of the tensor indices to the rows and the
% columns of the matrix.
%% Creating a tenmat (tensor as matrix) object
X = tensor(1:24,[3 2 2 2]) %<REPLACE_WITH_DASH_DASH Create a tensor.
%%
A = tenmat(X,[1 2],[3 4]) %<REPLACE_WITH_DASH_DASH Dims [1 2] map to rows, [3 4] to columns.
%%
B = tenmat(X,[2 1],[3 4]) %<REPLACE_WITH_DASH_DASH Order matters!
%%
C = tenmat(X,[1 2],[4 3]) %<REPLACE_WITH_DASH_DASH Order matters!
%% Creating a tenmat by specifying the dimensions mapped to the rows
% If just the row indices are specified, then the columns are arranged in
% increasing order.
A = tenmat(X,1) %<REPLACE_WITH_DASH_DASH Same as A = tenmat(X,1,2:4)
%% Creating a tenmat by specifying the dimensions mapped to the columns
% Likewise, just the columns can be specified if the 3rd argument is a 't'.
% The rows are arranged in increasing order.
A = tenmat(X, [2 3], 't') %<REPLACE_WITH_DASH_DASH Same as A = tenmat(X,[1 4],[2 3]).
%% Vectorize via tenmat
% All the dimensions can be mapped to the rows or the columnns.
A = tenmat(X,1:4,'t') %<REPLACE_WITH_DASH_DASH Map all the dimensions to the columns
%% Alternative ordering for the columns for mode-n matricization
% Mode-n matricization means that only mode n is mapped to the rows.
% Different column orderings are available.
A = tenmat(X,2) %<REPLACE_WITH_DASH_DASH By default, columns are ordered as [1 3 4].
%%
A = tenmat(X,2,[3 1 4]) %<REPLACE_WITH_DASH_DASH Explicit specification.
%%
A = tenmat(X,2,'fc') %<REPLACE_WITH_DASH_DASH Forward cyclic, i.e., [3 4 1].
%%
A = tenmat(X,2,'bc') %<REPLACE_WITH_DASH_DASH Backward cyclic, i.e., [1 4 3].
%% Constituent parts of a tenmat
A.data %<REPLACE_WITH_DASH_DASH The matrix itself.
%%
A.tsize %<REPLACE_WITH_DASH_DASH Size of the original tensor.
%%
A.rdims %<REPLACE_WITH_DASH_DASH Dimensions that were mapped to the rows.
%%
A.cdims %<REPLACE_WITH_DASH_DASH Dimensions that were mapped to the columns.
%% Creating a tenmat from its constituent parts
B = tenmat(A.data,A.rdims,A.cdims,A.tsize) %<REPLACE_WITH_DASH_DASH Recreates A
%% Creating an empty tenmat
B = tenmat %<REPLACE_WITH_DASH_DASH Empty tenmat.
%% Use double to convert a tenmat to a MATLAB matrix
double(A) %<REPLACE_WITH_DASH_DASH converts A to a standard matrix
%% Use tensor to convert a tenmat to a tensor
Y = tensor(A)
%% Use size and tsize for the dimensions of a tenmat
size(A) %<REPLACE_WITH_DASH_DASH Matrix size
tsize(A) %<REPLACE_WITH_DASH_DASH Corresponding tensor size
%% Subscripted reference for a tenmat
A(2,1) %<REPLACE_WITH_DASH_DASH returns the (2,1) element of the matrix.
%% Subscripted assignment for a tenmat
A(1:2,1:2) = ones(2) %<REPLACE_WITH_DASH_DASH Replace part of the matrix.
%% Use end for the last index
A(end,end) %<REPLACE_WITH_DASH_DASH Same as X(2,12)
%% Basic operations for tenmat
norm(A) %<REPLACE_WITH_DASH_DASH Norm of the matrix.
%%
A' %<REPLACE_WITH_DASH_DASH Calls ctranspose (also swaps mapped dimensions).
%%
+A %<REPLACE_WITH_DASH_DASH Calls uplus.
%%
-A %<REPLACE_WITH_DASH_DASH Calls uminus.
%%
A+A %<REPLACE_WITH_DASH_DASH Calls plus.
%%
A-A %<REPLACE_WITH_DASH_DASH Calls minus.
%% Multiplying two tenmats
% It is possible to compute the product of two tenmats and have a result
% that can be converted into a tensor.
B = A * A' %<REPLACE_WITH_DASH_DASH Tenmat that is the product of two tenmats.
%%
tensor(B) %<REPLACE_WITH_DASH_DASH Corresponding tensor.
%% Displaying a tenmat
% Shows the original tensor dimensions, the modes mapped to rows, the modes
% mapped to columns, and the matrix.
disp(A)
##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -