b2_sptenmat_doc.html
来自「张量分析工具」· HTML 代码 · 共 612 行 · 第 1/2 页
HTML
612 行
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Converting sparse tensors to matrices and vice versa</title> <meta name="generator" content="MATLAB 7.2"> <meta name="date" content="2007-01-10"> <meta name="m-file" content="B2_sptenmat_doc"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. On Gecko-based browsers, the shrink-to-fit doesn't work. */ p,h1,h2,div.content div { /* for MATLAB's browser */ width: 600px; /* for Mozilla, but the "width" tag overrides it anyway */ max-width: 600px; /* for IE */ width:expression(document.body.clientWidth > 620 ? "600px": "auto" );}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>Converting sparse tensors to matrices and vice versa</h1> <introduction> <p>We show how to convert a sptensor to a matrix stored in <i>coordinate</i> format and with extra information so that it can be converted back to a sptensor. </p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Creating a sptenmat (sparse tensor as sparse matrix) object</a></li> <li><a href="#11">Constituent parts of a sptenmat</a></li> <li><a href="#16">Creating a sptenmat from its constituent parts</a></li> <li><a href="#18">Creating a sptenmat with no nonzeros</a></li> <li><a href="#19">Creating an emtpy sptenmat</a></li> <li><a href="#20">Use double to convert a sptenmat to a MATLAB sparse matrix</a></li> <li><a href="#24">Use full to convert a sptenmath to a tenmat</a></li> <li><a href="#26">Use sptensor to convert a sptenmat to a sptensor</a></li> <li><a href="#27">Use size and tsize for the dimensions of a sptenmat</a></li> <li><a href="#28">Subscripted reference for a sptenmat</a></li> <li><a href="#29">Subscripted assignment for a sptenmat</a></li> <li><a href="#30">Use end for the last index</a></li> <li><a href="#31">Basic operations for sptenmat</a></li> <li><a href="#34">Use aatx to efficiently compute A * A' * x for a sptenmat</a></li> <li><a href="#36">Displaying a tenmat</a></li> </ul> </div> <h2>Creating a sptenmat (sparse tensor as sparse matrix) object<a name="1"></a></h2> <p>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. </p> <p>First, we create a sparse tensor to be converted.</p><pre class="codeinput">X = sptenrand([10 10 10 10],10) <span class="comment">%<-- Generate some data.</span></pre><pre class="codeoutput">X is a sparse tensor of size 10 x 10 x 10 x 10 with 10 nonzeros (1,9, 5, 1) 0.4249 (3,2, 1, 2) 0.3756 (4,7,10, 9) 0.1662 (4,8, 3,10) 0.8332 (5,3, 4, 8) 0.8386 (5,3, 6, 7) 0.4516 (6,7, 5, 1) 0.9566 (6,7, 6, 6) 0.1472 (7,8, 5, 6) 0.8699 (9,9, 1, 9) 0.7694</pre><p>All the same options for tenmat are available as for tenmat.</p><pre class="codeinput">A = sptenmat(X,1) <span class="comment">%<-- Mode-1 matricization.</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) (1, 49) 0.424889 (3,102) 0.375577 (4,897) 0.166154 (4,928) 0.833151 (5,733) 0.83864 (5,653) 0.451614 (6, 47) 0.956601 (6,557) 0.147153 (7,548) 0.869933 (9,809) 0.769436</pre><pre class="codeinput">A = sptenmat(X,[2 3]) <span class="comment">%<-- More than one mode is mapped to the columns.</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 = [ 2 3 ] (modes of tensor corresponding to rows) A.cindices = [ 1 4 ] (modes of tensor corresponding to columns) (49, 1) 0.424889 ( 2,13) 0.375577 (97,84) 0.166154 (28,94) 0.833151 (33,75) 0.83864 (53,65) 0.451614 (47, 6) 0.956601 (57,56) 0.147153 (48,57) 0.869933 ( 9,89) 0.769436</pre><pre class="codeinput">A = sptenmat(X,[2 3],<span class="string">'t'</span>) <span class="comment">%<-- Specify column dimensions (transpose).</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 4 ] (modes of tensor corresponding to rows) A.cindices = [ 2 3 ] (modes of tensor corresponding to columns) ( 1,49) 0.424889 (13, 2) 0.375577 (84,97) 0.166154 (94,28) 0.833151 (75,33) 0.83864 (65,53) 0.451614 ( 6,47) 0.956601 (56,57) 0.147153 (57,48) 0.869933 (89, 9) 0.769436</pre><pre class="codeinput">A = sptenmat(X,1:4) <span class="comment">%<-- All modes mapped to rows, i.e., vectorize.</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 2 3 4 ] (modes of tensor corresponding to rows) A.cindices = [ ] (modes of tensor corresponding to columns) ( 481,1) 0.424889 (1013,1) 0.375577 (8964,1) 0.166154 (9274,1) 0.833151 (7325,1) 0.83864 (6525,1) 0.451614 ( 466,1) 0.956601 (5566,1) 0.147153 (5477,1) 0.869933 (8089,1) 0.769436</pre><pre class="codeinput">A = sptenmat(X,2) <span class="comment">%<-- By default, columns are ordered as [1 3 4].</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 = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 1 3 4 ] (modes of tensor corresponding to columns) (9, 41) 0.424889 (2,103) 0.375577 (7,894) 0.166154 (8,924) 0.833151 (3,735) 0.83864 (3,655) 0.451614 (7, 46) 0.956601 (7,556) 0.147153 (8,547) 0.869933 (9,809) 0.769436</pre><pre class="codeinput">A = sptenmat(X,2,[3 1 4]) <span class="comment">%<-- Explicit column ordering.</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 = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 3 1 4 ] (modes of tensor corresponding to columns) (9, 5) 0.424889 (2,121) 0.375577 (7,840) 0.166154 (8,933) 0.833151 (3,744) 0.83864 (3,646) 0.451614 (7, 55) 0.956601 (7,556) 0.147153 (8,565) 0.869933 (9,881) 0.769436</pre><pre class="codeinput">A = sptenmat(X,2,<span class="string">'fc'</span>) <span class="comment">%<-- Foward cyclic.</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 = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 3 4 1 ] (modes of tensor corresponding to columns) (9, 5) 0.424889 (2,211) 0.375577 (7,390) 0.166154 (8,393) 0.833151 (3,474) 0.83864 (3,466) 0.451614 (7,505) 0.956601 (7,556) 0.147153 (8,655) 0.869933 (9,881) 0.769436</pre><pre class="codeinput">A = sptenmat(X,2,<span class="string">'bc'</span>) <span class="comment">%<-- Backward cyclic.</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 = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) (9,401) 0.424889 (2, 13) 0.375577 (7,984) 0.166154 (8,294) 0.833151 (3,375) 0.83864 (3,565) 0.451614 (7,406) 0.956601 (7,556) 0.147153 (8,457) 0.869933 (9, 89) 0.769436</pre><h2>Constituent parts of a sptenmat<a name="11"></a></h2><pre class="codeinput">A.subs <span class="comment">%<-- Subscripts of the nonzeros.</span></pre><pre class="codeoutput">ans = 9 401 2 13 7 984 8 294 3 375 3 565 7 406 7 556 8 457 9 89</pre><pre class="codeinput">A.vals <span class="comment">%<-- The corresponding nonzero values.</span></pre><pre class="codeoutput">ans = 0.4249 0.3756 0.1662 0.8332 0.8386 0.4516 0.9566 0.1472 0.8699 0.7694</pre><pre class="codeinput">A.tsize <span class="comment">%<-- Size of the original tensor.</span></pre><pre class="codeoutput">ans = 10 10 10 10</pre><pre class="codeinput">A.rdims <span class="comment">%<-- Dimensions that were mapped to the rows.</span></pre><pre class="codeoutput">ans = 2</pre><pre class="codeinput">A.cdims <span class="comment">%<-- Dimensions that were mapped to the columns.</span></pre><pre class="codeoutput">ans = 1 4 3</pre><h2>Creating a sptenmat from its constituent parts<a name="16"></a></h2><pre class="codeinput">B = sptenmat(A.subs,A.vals,A.rdims,A.cdims,A.tsize) <span class="comment">%<-- Copies A</span></pre><pre class="codeoutput">B is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros B.rindices = [ 2 ] (modes of tensor corresponding to rows) B.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) (2, 13) 0.375577 (3,375) 0.83864 (3,565) 0.451614 (7,406) 0.956601 (7,556) 0.147153 (7,984) 0.166154 (8,294) 0.833151 (8,457) 0.869933 (9, 89) 0.769436 (9,401) 0.424889</pre><pre class="codeinput">B = sptenmat(double(A),A.rdims,A.cdims,A.tsize) <span class="comment">%<-- More efficient to pass a matrix.</span></pre><pre class="codeoutput">B is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros B.rindices = [ 2 ] (modes of tensor corresponding to rows) B.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns) (2, 13) 0.375577 (9, 89) 0.769436 (8,294) 0.833151 (3,375) 0.83864 (9,401) 0.424889 (7,406) 0.956601 (8,457) 0.869933 (7,556) 0.147153 (3,565) 0.451614 (7,984) 0.166154</pre><h2>Creating a sptenmat with no nonzeros<a name="18"></a></h2><pre class="codeinput">A = sptenmat([],[],A.rdims,A.cdims,A.tsize) <span class="comment">%<-- An empty sptenmat.</span></pre><pre class="codeoutput">A is an all-zero sptenmat from an sptensor of size 10 x 10 x 10 x 10 A.rindices = [ 2 ] (modes of tensor corresponding to rows) A.cindices = [ 1 4 3 ] (modes of tensor corresponding to columns)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?