b2_sptenmat_doc.html
来自「张量分析工具」· HTML 代码 · 共 612 行 · 第 1/2 页
HTML
612 行
</pre><h2>Creating an emtpy sptenmat<a name="19"></a></h2><pre class="codeinput">A = sptenmat <span class="comment">%<-- A really empty sptenmat.</span></pre><pre class="codeoutput">A is an all-zero sptenmat from an sptensor of size [empty tensor] A.rindices = [ ] (modes of tensor corresponding to rows) A.cindices = [ ] (modes of tensor corresponding to columns)</pre><h2>Use double to convert a sptenmat to a MATLAB sparse matrix<a name="20"></a></h2><pre class="codeinput">X = sptenrand([10 10 10 10],10); <span class="comment">%<-- Create a tensor.</span>A = sptenmat(X,1) <span class="comment">%<-- Convert it to a sptenmat</span></pre><pre class="codeoutput">A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros A.rindices = [ 1 ] (modes of tensor corresponding to rows) A.cindices = [ 2 3 4 ] (modes of tensor corresponding to columns) ( 2,723) 0.783859 ( 3, 32) 0.986158 ( 4,647) 0.473343 ( 5,775) 0.902819 ( 5,236) 0.451059 ( 5,256) 0.804517 ( 5,357) 0.828864 ( 7,191) 0.16627 ( 7,447) 0.393906 (10,434) 0.520757</pre><pre class="codeinput">B = double(A) <span class="comment">%<-- Convert it to a MATLAB sparse matrix</span></pre><pre class="codeoutput">B = (3,32) 0.9862 (7,191) 0.1663 (5,236) 0.4511 (5,256) 0.8045 (5,357) 0.8289 (10,434) 0.5208 (7,447) 0.3939 (4,647) 0.4733 (2,723) 0.7839 (5,775) 0.9028</pre><pre class="codeinput">whos <span class="string">A</span> <span class="string">B</span> <span class="comment">%<-- The storage for B (the sparse matrix) is larger than for A.</span></pre><pre class="codeoutput"> Name Size Bytes Class A 10x1000 924 sptenmat object B 10x1000 4124 double array (sparse)Grand total is 53 elements using 5048 bytes</pre><pre class="codeinput">C = B'; <span class="comment">%<-- Transposing the result fixes the problem.</span>whos <span class="string">C</span></pre><pre class="codeoutput"> Name Size Bytes Class C 1000x10 164 double array (sparse)Grand total is 10 elements using 164 bytes</pre><h2>Use full to convert a sptenmath to a tenmat<a name="24"></a></h2><pre class="codeinput">B = sptenmat(sptenrand([3 3 3], 3), 1) <span class="comment">%<-- Create a sptenmat</span></pre><pre class="codeoutput">B is a sptenmat from an sptensor of size 3 x 3 x 3 with 3 nonzeros B.rindices = [ 1 ] (modes of tensor corresponding to rows) B.cindices = [ 2 3 ] (modes of tensor corresponding to columns) (2,1) 0.759479 (2,8) 0.949759 (3,5) 0.557939</pre><pre class="codeinput">C = full(B) <span class="comment">%<-- Convert to a tenmat</span></pre><pre class="codeoutput">C is a matrix corresponding to a tensor of size 3 x 3 x 3 C.rindices = [ 1 ] (modes of tensor corresponding to rows) C.cindices = [ 2 3 ] (modes of tensor corresponding to columns) C.data = Columns 1 through 8 0 0 0 0 0 0 0 0 0.7595 0 0 0 0 0 0 0.9498 0 0 0 0 0.5579 0 0 0 Column 9 0 0 0</pre><h2>Use sptensor to convert a sptenmat to a sptensor<a name="26"></a></h2><pre class="codeinput">Y = sptensor(A) <span class="comment">%<-- Convert a sptenmat to a sptensor</span></pre><pre class="codeoutput">Y is a sparse tensor of size 10 x 10 x 10 x 10 with 10 nonzeros ( 2,3, 3,8) 0.7839 ( 3,2, 4,1) 0.9862 ( 4,7, 5,7) 0.4733 ( 5,5, 8,8) 0.9028 ( 5,6, 4,3) 0.4511 ( 5,6, 6,3) 0.8045 ( 5,7, 6,4) 0.8289 ( 7,1,10,2) 0.1663 ( 7,7, 5,5) 0.3939 (10,4, 4,5) 0.5208</pre><h2>Use size and tsize for the dimensions of a sptenmat<a name="27"></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 = 10 1000ans = 10 10 10 10</pre><h2>Subscripted reference for a sptenmat<a name="28"></a></h2> <p>This is not supported beyond getting the constituent parts.</p> <h2>Subscripted assignment for a sptenmat<a name="29"></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 sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros A.rindices = [ 1 ] (modes of tensor corresponding to rows) A.cindices = [ 2 3 4 ] (modes of tensor corresponding to columns) ( 1, 1) 1 ( 1, 2) 1 ( 2, 1) 1 ( 2, 2) 1 ( 2,723) 0.783859 ( 3, 32) 0.986158 ( 4,647) 0.473343 ( 5,236) 0.451059 ( 5,256) 0.804517 ( 5,357) 0.828864 ( 5,775) 0.902819 ( 7,191) 0.16627 ( 7,447) 0.393906 (10,434) 0.520757</pre><h2>Use end for the last index<a name="30"></a></h2> <p>End is not supported.</p> <h2>Basic operations for sptenmat<a name="31"></a></h2><pre class="codeinput">norm(A) <span class="comment">%<-- Norm of the matrix.</span></pre><pre class="codeoutput">ans = 2.9356</pre><pre class="codeinput">+A <span class="comment">%<-- Calls uplus.</span></pre><pre class="codeoutput">ans is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros ans.rindices = [ 1 ] (modes of tensor corresponding to rows) ans.cindices = [ 2 3 4 ] (modes of tensor corresponding to columns) ( 1, 1) 1 ( 1, 2) 1 ( 2, 1) 1 ( 2, 2) 1 ( 2,723) 0.783859 ( 3, 32) 0.986158 ( 4,647) 0.473343 ( 5,236) 0.451059 ( 5,256) 0.804517 ( 5,357) 0.828864 ( 5,775) 0.902819 ( 7,191) 0.16627 ( 7,447) 0.393906 (10,434) 0.520757</pre><pre class="codeinput">-A <span class="comment">%<-- Calls uminus.</span></pre><pre class="codeoutput">ans is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros ans.rindices = [ 1 ] (modes of tensor corresponding to rows) ans.cindices = [ 2 3 4 ] (modes of tensor corresponding to columns) ( 1, 1) -1 ( 1, 2) -1 ( 2, 1) -1 ( 2, 2) -1 ( 2,723) -0.783859 ( 3, 32) -0.986158 ( 4,647) -0.473343 ( 5,236) -0.451059 ( 5,256) -0.804517 ( 5,357) -0.828864 ( 5,775) -0.902819 ( 7,191) -0.16627 ( 7,447) -0.393906 (10,434) -0.520757</pre><h2>Use aatx to efficiently compute A * A' * x for a sptenmat<a name="34"></a></h2><pre class="codeinput">x = ones(10,1); <span class="comment">%<-- Create vector</span>aatx(A,x) <span class="comment">%<-- Compute A * A' * x</span></pre><pre class="codeoutput">ans = 4.0000 4.6144 0.9725 0.2241 2.3528 0 0.1828 0 0 0.2712</pre><pre class="codeinput">double(A) * double(A)' * x <span class="comment">%<-- Same as above but less efficient</span></pre><pre class="codeoutput">ans = 4.0000 4.6144 0.9725 0.2241 2.3528 0 0.1828 0 0 0.2712</pre><h2>Displaying a tenmat<a name="36"></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 sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros ans.rindices = [ 1 ] (modes of tensor corresponding to rows) ans.cindices = [ 2 3 4 ] (modes of tensor corresponding to columns) ( 1, 1) 1 ( 1, 2) 1 ( 2, 1) 1 ( 2, 2) 1 ( 2,723) 0.783859 ( 3, 32) 0.986158 ( 4,647) 0.473343 ( 5,236) 0.451059 ( 5,256) 0.804517 ( 5,357) 0.828864 ( 5,775) 0.902819 ( 7,191) 0.16627 ( 7,447) 0.393906 (10,434) 0.520757</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Converting sparse tensors to matrices and vice versa
% We show how to convert a sptensor to a matrix stored in _coordinate_
% format and with extra information so that it can be converted back to a
% sptensor.
%% Creating a sptenmat (sparse tensor as sparse matrix) object
% A sparse tensor can be converted to a sparse matrix. The matrix, however,
% is not stored as a MATLAB sparse matrix because that format is sometimes
% inefficient for converted sparse tensors. Instead, the row and column
% indices are stored explicitly.
%%
% First, we create a sparse tensor to be converted.
X = sptenrand([10 10 10 10],10) %<REPLACE_WITH_DASH_DASH Generate some data.
%%
% All the same options for tenmat are available as for tenmat.
A = sptenmat(X,1) %<REPLACE_WITH_DASH_DASH Mode-1 matricization.
%%
A = sptenmat(X,[2 3]) %<REPLACE_WITH_DASH_DASH More than one mode is mapped to the columns.
%%
A = sptenmat(X,[2 3],'t') %<REPLACE_WITH_DASH_DASH Specify column dimensions (transpose).
%%
A = sptenmat(X,1:4) %<REPLACE_WITH_DASH_DASH All modes mapped to rows, i.e., vectorize.
%%
A = sptenmat(X,2) %<REPLACE_WITH_DASH_DASH By default, columns are ordered as [1 3 4].
%%
A = sptenmat(X,2,[3 1 4]) %<REPLACE_WITH_DASH_DASH Explicit column ordering.
%%
A = sptenmat(X,2,'fc') %<REPLACE_WITH_DASH_DASH Foward cyclic.
%%
A = sptenmat(X,2,'bc') %<REPLACE_WITH_DASH_DASH Backward cyclic.
%% Constituent parts of a sptenmat
A.subs %<REPLACE_WITH_DASH_DASH Subscripts of the nonzeros.
%%
A.vals %<REPLACE_WITH_DASH_DASH The corresponding nonzero values.
%%
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 sptenmat from its constituent parts
B = sptenmat(A.subs,A.vals,A.rdims,A.cdims,A.tsize) %<REPLACE_WITH_DASH_DASH Copies A
%%
B = sptenmat(double(A),A.rdims,A.cdims,A.tsize) %<REPLACE_WITH_DASH_DASH More efficient to pass a matrix.
%% Creating a sptenmat with no nonzeros
A = sptenmat([],[],A.rdims,A.cdims,A.tsize) %<REPLACE_WITH_DASH_DASH An empty sptenmat.
%% Creating an emtpy sptenmat
A = sptenmat %<REPLACE_WITH_DASH_DASH A really empty sptenmat.
%% Use double to convert a sptenmat to a MATLAB sparse matrix
X = sptenrand([10 10 10 10],10); %<REPLACE_WITH_DASH_DASH Create a tensor.
A = sptenmat(X,1) %<REPLACE_WITH_DASH_DASH Convert it to a sptenmat
%%
B = double(A) %<REPLACE_WITH_DASH_DASH Convert it to a MATLAB sparse matrix
%%
whos A B %<REPLACE_WITH_DASH_DASH The storage for B (the sparse matrix) is larger than for A.
%%
C = B'; %<REPLACE_WITH_DASH_DASH Transposing the result fixes the problem.
whos C
%% Use full to convert a sptenmath to a tenmat
B = sptenmat(sptenrand([3 3 3], 3), 1) %<REPLACE_WITH_DASH_DASH Create a sptenmat
%%
C = full(B) %<REPLACE_WITH_DASH_DASH Convert to a tenmat
%% Use sptensor to convert a sptenmat to a sptensor
Y = sptensor(A) %<REPLACE_WITH_DASH_DASH Convert a sptenmat to a sptensor
%% Use size and tsize for the dimensions of a sptenmat
size(A) %<REPLACE_WITH_DASH_DASH Matrix size
tsize(A) %<REPLACE_WITH_DASH_DASH Corresponding tensor size
%% Subscripted reference for a sptenmat
% This is not supported beyond getting the constituent parts.
%% Subscripted assignment for a sptenmat
A(1:2,1:2) = ones(2) %<REPLACE_WITH_DASH_DASH Replace part of the matrix.
%% Use end for the last index
% End is not supported.
%% Basic operations for sptenmat
norm(A) %<REPLACE_WITH_DASH_DASH Norm of the matrix.
%%
+A %<REPLACE_WITH_DASH_DASH Calls uplus.
%%
-A %<REPLACE_WITH_DASH_DASH Calls uminus.
%% Use aatx to efficiently compute A * A' * x for a sptenmat
x = ones(10,1); %<REPLACE_WITH_DASH_DASH Create vector
aatx(A,x) %<REPLACE_WITH_DASH_DASH Compute A * A' * x
%%
double(A) * double(A)' * x %<REPLACE_WITH_DASH_DASH Same as above but less efficient
%% 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 + =
减小字号Ctrl + -
显示快捷键?