⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a2_sptensor_doc.html

📁 张量分析工具
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<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>Sparse Tensors</title>      <meta name="generator" content="MATLAB 7.2">      <meta name="date" content="2007-01-10">      <meta name="m-file" content="A2_sptensor_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>Sparse Tensors</h1>         <introduction>            <p>MATLAB has no native ability to store sparse multidimensional arrays, only sparse matrices. Moreover, the compressed sparse               column storage format for MATLAB sparse matrices is not readily adaptable to sparse tensors. Instead, the <tt>sptensor</tt> class stores the data in coordinate format.            </p>         </introduction>         <h2>Contents</h2>         <div>            <ul>               <li><a href="#1">Creating a sptensor</a></li>               <li><a href="#4">Specifying the accumulation method for the constructor</a></li>               <li><a href="#6">Creating a one-dimensional sptensor.</a></li>               <li><a href="#8">Creating an all-zero sptensor</a></li>               <li><a href="#10">Constituent parts of a sptensor</a></li>               <li><a href="#13">Creating a sparse tensor from its constituent parts</a></li>               <li><a href="#14">Creating an empty sptensor</a></li>               <li><a href="#15">Use sptenrand to create a random sptensor</a></li>               <li><a href="#17">Use squeeze to remove singleton dimensions from a sptensor</a></li>               <li><a href="#18">Use full or tensor to convert a sptensor to a (dense) tensor</a></li>               <li><a href="#20">Use sptensor to convert a (dense) tensor to a sptensor</a></li>               <li><a href="#21">Use double to convert a sptensor to a (dense) multidimensional array</a></li>               <li><a href="#22">Use find to extract nonzeros from a tensor and then create a sptensor</a></li>               <li><a href="#23">Use ndims and size to get the size of a sptensor</a></li>               <li><a href="#26">Use nnz to get the number of nonzeros of a sptensor</a></li>               <li><a href="#27">Subscripted reference for a sptensor</a></li>               <li><a href="#37">Subscripted assignment for a sptensor</a></li>               <li><a href="#43">Use end as the last index.</a></li>               <li><a href="#44">Use elemfun to manipulate the nonzeros of a sptensor</a></li>               <li><a href="#49">Basic operations (plus, minus, times, etc.) on a sptensor</a></li>               <li><a href="#59">Use permute to reorder the modes of a sptensor</a></li>               <li><a href="#63">Displaying a tensor</a></li>            </ul>         </div>         <h2>Creating a sptensor<a name="1"></a></h2>         <p>A sparse tensor can be created by passing in a list of subscripts and values. For example, here we pass in three subscripts            and a scalar value. The resuling sparse tensor has three nonzero entries, and the size is the size of the largest subscript            in each dimension.         </p><pre class="codeinput">rand(<span class="string">'state'</span>,0); <span class="comment">%&lt;-- Setup for the script</span>subs = [1,1,1;1,2,1;3,4,2]; <span class="comment">%&lt;-- Subscripts of the nonzeros.</span>vals = [1; 2; 3]; <span class="comment">%&lt;-- The values of the nonzeros.</span>X = sptensor(subs,vals) <span class="comment">%&lt;-- Create a sparse tensor with 3 nonzeros.</span></pre><pre class="codeoutput">X is a sparse tensor of size 3 x 4 x 2 with 3 nonzeros	(1,1,1)     1	(1,2,1)     2	(3,4,2)     3</pre><pre class="codeinput">X = sptensor(subs,vals,[3 5 2]) <span class="comment">%&lt;-- Or, specify the size explicitly.</span></pre><pre class="codeoutput">X is a sparse tensor of size 3 x 5 x 2 with 3 nonzeros	(1,1,1)     1	(1,2,1)     2	(3,4,2)     3</pre><p>Values corresponding to repeated subscripts are summed. Also note that we can use a scalar as the second argument.</p><pre class="codeinput">subs = [1 1 1; 1 1 3; 2 2 2; 4 4 4; 1 1 1; 1 1 1]; <span class="comment">%&lt;-- (1,1,1) is repeated.</span>X = sptensor(subs,2) <span class="comment">%&lt;-- Equivalent to X = sptensor(subs,2*ones(6,1)).</span></pre><pre class="codeoutput">X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros	(1,1,1)     6	(1,1,3)     2	(2,2,2)     2	(4,4,4)     2</pre><h2>Specifying the accumulation method for the constructor<a name="4"></a></h2>         <p>By default, values corresponding to repeated elements are summed. However, it is possible to specify other actions to be taken.</p><pre class="codeinput">X = sptensor(subs,2*ones(6,1),[4 4 4],@max) <span class="comment">%&lt;-- Maximum element.</span></pre><pre class="codeoutput">X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros	(1,1,1)     2	(1,1,3)     2	(2,2,2)     2	(4,4,4)     2</pre><pre class="codeinput">myfun = @(x) sum(x) / 3; <span class="comment">%&lt;-- Total sum divided by three.</span>X = sptensor(subs,2*ones(6,1),[4 4 4],myfun) <span class="comment">%&lt;-- Custom accumulation function.</span></pre><pre class="codeoutput">X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros	(1,1,1)    2.0000	(1,1,3)    0.6667	(2,2,2)    0.6667	(4,4,4)    0.6667</pre><h2>Creating a one-dimensional sptensor.<a name="6"></a></h2><pre class="codeinput">X = sptensor([1;3;5],1,10) <span class="comment">%&lt;-- Same as X = sptensor([1;3;5],[1;1;1],1,10).</span></pre><pre class="codeoutput">X is a sparse tensor of size 10 with 3 nonzeros	(1)     1	(3)     1	(5)     1</pre><pre class="codeinput">X = sptenrand(50,5) <span class="comment">%&lt;-- A random, sparse, order-1 tensor with 5 nonzeros.</span></pre><pre class="codeoutput">X is a sparse tensor of size 50 with 5 nonzeros	(12)    0.7621	(25)    0.4565	(31)    0.0185	(45)    0.8214	(48)    0.4447</pre><h2>Creating an all-zero sptensor<a name="8"></a></h2><pre class="codeinput">X = sptensor([],[],[10 10 10]) <span class="comment">%&lt;-- Creates an all-zero tensor.</span></pre><pre class="codeoutput">X is an all-zero sparse tensor of size 10 x 10 x 10</pre><pre class="codeinput">X = sptensor([10 10 10]) <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">X is an all-zero sparse tensor of size 10 x 10 x 10</pre><h2>Constituent parts of a sptensor<a name="10"></a></h2><pre class="codeinput">X = sptenrand([40 30 20],5); <span class="comment">%&lt;-- Create data.</span>X.subs <span class="comment">%&lt;-- Subscripts of nonzeros.</span></pre><pre class="codeoutput">ans =     8    27     3    25    13     2    30    13     1    32    29     8    37    28    17</pre><pre class="codeinput">X.vals <span class="comment">%&lt;-- Corresponding nonzero values.</span></pre><pre class="codeoutput">ans =    0.2028    0.1987    0.6038    0.2722    0.1988</pre><pre class="codeinput">X.size <span class="comment">%&lt;-- The size.</span></pre><pre class="codeoutput">ans =    40    30    20</pre><h2>Creating a sparse tensor from its constituent parts<a name="13"></a></h2><pre class="codeinput">Y = sptensor(X.subs,X.vals,X.size) <span class="comment">%&lt;-- Copies X.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 40 x 30 x 20 with 5 nonzeros	( 8,27, 3)    0.2028	(25,13, 2)    0.1987	(30,13, 1)    0.6038	(32,29, 8)    0.2722	(37,28,17)    0.1988</pre><h2>Creating an empty sptensor<a name="14"></a></h2>         <p>An empty constructor exists, primarily to support loads of previously saved data.</p><pre class="codeinput">Y = sptensor <span class="comment">%&lt;-- Create an empty sptensor.</span></pre><pre class="codeoutput">Y is an all-zero sparse tensor of size [empty tensor]</pre><h2>Use sptenrand to create a random sptensor<a name="15"></a></h2><pre class="codeinput">X = sptenrand([10 10 10],0.01) <span class="comment">%&lt;-- Create a tensor with 1% nonzeroes.</span></pre><pre class="codeoutput">X is a sparse tensor of size 10 x 10 x 10 with 10 nonzeros	( 1,9,2)    0.4966	( 3,4,9)    0.8998	( 5,6,7)    0.8216	( 5,7,4)    0.6449	( 5,9,2)    0.8180	( 6,5,9)    0.6602	( 7,2,6)    0.3420	( 8,1,7)    0.2897	( 9,8,4)    0.3412	(10,4,6)    0.5341</pre><p>It is also posible to specify the precise number of nonzeros rather than a percentage.</p><pre class="codeinput">X = sptenrand([10 10 10],10) <span class="comment">%&lt;-- Create a tensor with 10 nonzeros.</span></pre><pre class="codeoutput">X is a sparse tensor of size 10 x 10 x 10 with 10 nonzeros	(4, 2, 3)    0.5828	(4,10, 1)    0.4235	(5, 3, 5)    0.5155	(6, 3, 3)    0.3340	(6, 9, 2)    0.4329	(7, 8,10)    0.2259	(7, 9, 1)    0.5798	(8, 8, 2)    0.7604	(8,10, 7)    0.5298	(9, 6, 9)    0.6405</pre><h2>Use squeeze to remove singleton dimensions from a sptensor<a name="17"></a></h2><pre class="codeinput">Y = sptensor([1 1 1; 2 1 1], 1, [2 1 1]) <span class="comment">%&lt;-- Create a sparse tensor.</span>squeeze(Y) <span class="comment">%&lt;-- Remove singleton dimensions.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 2 x 1 x 1 with 2 nonzeros	(1,1,1)     1	(2,1,1)     1ans is a sparse tensor of size 2 with 2 nonzeros	(1)     1	(2)     1</pre><h2>Use full or tensor to convert a sptensor to a (dense) tensor<a name="18"></a></h2><pre class="codeinput">X = sptensor([1 1 1; 2 2 2], [1; 1]); <span class="comment">%&lt;-- Create a sparse tensor.</span>Y = full(X) <span class="comment">%&lt;-- Convert it to a (dense) tensor.</span></pre><pre class="codeoutput">Y is a tensor of size 2 x 2 x 2	Y(:,:,1) = 	     1     0	     0     0	Y(:,:,2) = 	     0     0	     0     1</pre><pre class="codeinput">Y = tensor(X) <span class="comment">%&lt;-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 2 x 2 x 2	Y(:,:,1) = 	     1     0	     0     0	Y(:,:,2) = 	     0     0	     0     1</pre><h2>Use sptensor to convert a (dense) tensor to a sptensor<a name="20"></a></h2><pre class="codeinput">Z = sptensor(Y) <span class="comment">%&lt;-- Convert a tensor to a sptensor.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 2 x 2 x 2 with 2 nonzeros	(1,1,1)     1	(2,2,2)     1</pre><h2>Use double to convert a sptensor to a (dense) multidimensional array<a name="21"></a></h2><pre class="codeinput">Y = double(X) <span class="comment">%&lt;-- Creates a MATLAB array.</span></pre><pre class="codeoutput">Y(:,:,1) =     1     0     0     0Y(:,:,2) =     0     0     0     1</pre><h2>Use find to extract nonzeros from a tensor and then create a sptensor<a name="22"></a></h2>         <p>The <tt>find</tt> command can be used to extract specific elements and then convert those into a sptensor.         </p><pre class="codeinput">X = tensor(rand(5,4,2),[5 4 2]) <span class="comment">%&lt;-- Create a tensor.</span>S = find(X &gt; 0.9) <span class="comment">%&lt;-- Extract subscipts of values greater than 0.9.</span>V = X(S) <span class="comment">%&lt;-- Extract the corresponding values.</span>Y = sptensor(S,V,[5 4 2]) <span class="comment">%&lt;-- Create a new tensor.</span></pre><pre class="codeoutput">X is a tensor of size 5 x 4 x 2	X(:,:,1) = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -