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

📄 a2_sptensor_doc.html

📁 张量分析工具
💻 HTML
📖 第 1 页 / 共 3 页
字号:
	    0.2091    0.5678    0.4154    0.9708	    0.3798    0.7942    0.3050    0.9901	    0.7833    0.0592    0.8744    0.7889	    0.6808    0.6029    0.0150    0.4387	    0.4611    0.0503    0.7680    0.4983	X(:,:,2) = 	    0.2140    0.4120    0.6833    0.2071	    0.6435    0.7446    0.2126    0.6072	    0.3200    0.2679    0.8392    0.6299	    0.9601    0.4399    0.6288    0.3705	    0.7266    0.9334    0.1338    0.5751S =     1     4     1     2     4     1     4     1     2     5     2     2V =    0.9708    0.9901    0.9601    0.9334Y is a sparse tensor of size 5 x 4 x 2 with 4 nonzeros	(1,4,1)    0.9708	(2,4,1)    0.9901	(4,1,2)    0.9601	(5,2,2)    0.9334</pre><h2>Use ndims and size to get the size of a sptensor<a name="23"></a></h2><pre class="codeinput">ndims(Y) <span class="comment">%&lt;-- Number of dimensions or modes.</span></pre><pre class="codeoutput">ans =     3</pre><pre class="codeinput">size(Y) <span class="comment">%&lt;-- Size of Y.</span></pre><pre class="codeoutput">ans =     5     4     2</pre><pre class="codeinput">size(Y,3) <span class="comment">%&lt;-- Size of mode 3 of Y.</span></pre><pre class="codeoutput">ans =     2</pre><h2>Use nnz to get the number of nonzeros of a sptensor<a name="26"></a></h2><pre class="codeinput">nnz(Y) <span class="comment">%&lt;-- Number of nonzeros in Y.</span></pre><pre class="codeoutput">ans =     4</pre><h2>Subscripted reference for a sptensor<a name="27"></a></h2><pre class="codeinput">X = sptensor([4,4,4;2,2,1;2,3,2],[3;5;1],[4 4 4]) <span class="comment">%&lt;-- Create a sptensor.</span></pre><pre class="codeoutput">X is a sparse tensor of size 4 x 4 x 4 with 3 nonzeros	(2,2,1)     5	(2,3,2)     1	(4,4,4)     3</pre><pre class="codeinput">X(1,2,1) <span class="comment">%&lt;-- Extract the (1,2,1) element, which is zero.</span></pre><pre class="codeoutput">ans =     0</pre><pre class="codeinput">X(4,4,4) <span class="comment">%&lt;-- Extract the (4,4,4) element, which is nonzero.</span></pre><pre class="codeoutput">ans =     3</pre><pre class="codeinput">X(1:2,2:4,:) <span class="comment">%&lt;-- Extract a 2 x 3 x 4 subtensor.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 3 x 4 with 2 nonzeros	(2,1,1)     5	(2,2,2)     1</pre><pre class="codeinput">X([1 1 1; 2 2 1]) <span class="comment">%&lt;-- Extract elements by subscript.</span></pre><pre class="codeoutput">ans =     0     5</pre><pre class="codeinput">X([1;6]) <span class="comment">%&lt;-- Same as above but with linear indices.</span></pre><pre class="codeoutput">ans =     0     5</pre><p>As with a tensor, subscriped reference may be ambiguous for one-dimensional tensors.</p><pre class="codeinput">X = sptensor([1;3;5],1,7) <span class="comment">%&lt;-- Create a sparse tensor.</span></pre><pre class="codeoutput">X is a sparse tensor of size 7 with 3 nonzeros	(1)     1	(3)     1	(5)     1</pre><pre class="codeinput">X(3) <span class="comment">%&lt;-- Fully specified, single elements are always returned as scalars.</span></pre><pre class="codeoutput">ans =     1</pre><pre class="codeinput">X([3;6]) <span class="comment">%&lt;-- Returns a subtensor.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 with 1 nonzeros	(1)     1</pre><pre class="codeinput">X([3;6],<span class="string">'extract'</span>) <span class="comment">%&lt;-- Same as above *but* returns an array.</span></pre><pre class="codeoutput">ans =     1     0</pre><h2>Subscripted assignment for a sptensor<a name="37"></a></h2><pre class="codeinput">X = sptensor([30 40 20]) <span class="comment">%&lt;-- Create an emtpy 30 x 40 x 20 sptensor.</span></pre><pre class="codeoutput">X is an all-zero sparse tensor of size 30 x 40 x 20</pre><pre class="codeinput">X(30,40,20) = 7 <span class="comment">%&lt;-- Assign a single element.</span></pre><pre class="codeoutput">X is a sparse tensor of size 30 x 40 x 20 with 1 nonzeros	(30,40,20)     7</pre><pre class="codeinput">X([1,1,1;2,2,2]) = [1;1] <span class="comment">%&lt;-- Assign a list of elements.</span></pre><pre class="codeoutput">X is a sparse tensor of size 30 x 40 x 20 with 3 nonzeros	(30,40,20)     7	( 1, 1, 1)     1	( 2, 2, 2)     1</pre><pre class="codeinput">X(11:20,11:20,11:20) = sptenrand([10,10,10],10) <span class="comment">%&lt;-- Assign a subtensor.</span></pre><pre class="codeoutput">X is a sparse tensor of size 30 x 40 x 20 with 13 nonzeros	(30,40,20)    7.0000	( 1, 1, 1)    1.0000	( 2, 2, 2)    1.0000	(12,13,15)    0.9342	(13,12,11)    0.2644	(13,12,16)    0.1603	(13,17,14)    0.8729	(15,13,14)    0.2379	(18,11,14)    0.6458	(19,11,14)    0.9669	(19,12,15)    0.6649	(19,19,12)    0.8704	(20,20,19)    0.0099</pre><pre class="codeinput">X(31,41,21) = 4 <span class="comment">%&lt;-- Grows the size of the sptensor.</span></pre><pre class="codeoutput">X is a sparse tensor of size 31 x 41 x 21 with 14 nonzeros	(30,40,20)    7.0000	( 1, 1, 1)    1.0000	( 2, 2, 2)    1.0000	(12,13,15)    0.9342	(13,12,11)    0.2644	(13,12,16)    0.1603	(13,17,14)    0.8729	(15,13,14)    0.2379	(18,11,14)    0.6458	(19,11,14)    0.9669	(19,12,15)    0.6649	(19,19,12)    0.8704	(20,20,19)    0.0099	(31,41,21)    4.0000</pre><pre class="codeinput">X(111:120,111:120,111:120) = sptenrand([10,10,10],10) <span class="comment">%&lt;-- Grow more.</span></pre><pre class="codeoutput">X is a sparse tensor of size 120 x 120 x 120 with 24 nonzeros	( 30, 40, 20)    7.0000	(  1,  1,  1)    1.0000	(  2,  2,  2)    1.0000	( 12, 13, 15)    0.9342	( 13, 12, 11)    0.2644	( 13, 12, 16)    0.1603	( 13, 17, 14)    0.8729	( 15, 13, 14)    0.2379	( 18, 11, 14)    0.6458	( 19, 11, 14)    0.9669	( 19, 12, 15)    0.6649	( 19, 19, 12)    0.8704	( 20, 20, 19)    0.0099	( 31, 41, 21)    4.0000	(112,111,118)    0.3759	(112,115,112)    0.0099	(112,115,113)    0.4199	(112,120,117)    0.7537	(114,115,115)    0.7939	(115,115,117)    0.9200	(117,115,116)    0.8447	(118,115,120)    0.3678	(119,119,111)    0.6208	(119,119,117)    0.7313</pre><h2>Use end as the last index.<a name="43"></a></h2><pre class="codeinput">X(end-10:end,end-10:end,end-5:end)  <span class="comment">%&lt;-- Same as X(108:118,110:120,115:120)</span></pre><pre class="codeoutput">ans is a sparse tensor of size 11 x 11 x 6 with 7 nonzeros	( 3, 2,4)    0.3759	( 3,11,3)    0.7537	( 5, 6,1)    0.7939	( 6, 6,3)    0.9200	( 8, 6,2)    0.8447	( 9, 6,6)    0.3678	(10,10,3)    0.7313</pre><h2>Use elemfun to manipulate the nonzeros of a sptensor<a name="44"></a></h2>         <p>The function <tt>elemfun</tt> is similar to <tt>spfun</tt> for sparse matrices.         </p><pre class="codeinput">X = sptenrand([10,10,10],3) <span class="comment">%&lt;-- Create some data.</span></pre><pre class="codeoutput">X is a sparse tensor of size 10 x 10 x 10 with 3 nonzeros	( 2,7,10)    0.3919	( 6,6, 7)    0.6273	(10,3, 4)    0.6991</pre><pre class="codeinput">Z = elemfun(X, @sqrt) <span class="comment">%&lt;-- Square root of every nonzero.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 10 x 10 x 10 with 3 nonzeros	( 2,7,10)    0.6260	( 6,6, 7)    0.7920	(10,3, 4)    0.8361</pre><pre class="codeinput">Z = elemfun(X, @(x) x+1) <span class="comment">%&lt;-- Use a custom function.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 10 x 10 x 10 with 3 nonzeros	( 2,7,10)    1.3919	( 6,6, 7)    1.6273	(10,3, 4)    1.6991</pre><pre class="codeinput">Z = elemfun(X, @(x) x~=0) <span class="comment">%&lt;-- Set every nonzero to one.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 10 x 10 x 10 with 3 nonzeros	( 2,7,10)     1	( 6,6, 7)     1	(10,3, 4)     1</pre><pre class="codeinput">Z = ones(X) <span class="comment">%&lt;-- An easier way to change every nonzero to one.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 10 x 10 x 10 with 3 nonzeros	( 2,7,10)     1	( 6,6, 7)     1	(10,3, 4)     1</pre><h2>Basic operations (plus, minus, times, etc.) on a sptensor<a name="49"></a></h2><pre class="codeinput">A = sptensor(tensor(floor(5*rand(2,2,2)))) <span class="comment">%&lt;-- Create data.</span>B = sptensor(tensor(floor(5*rand(2,2,2)))) <span class="comment">%&lt;-- Create more data.</span></pre><pre class="codeoutput">A is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)     1	(2,1,1)     2	(1,2,1)     3	(2,2,1)     4	(1,1,2)     1	(2,1,2)     2	(1,2,2)     2	(2,2,2)     2B is a sparse tensor of size 2 x 2 x 2 with 7 nonzeros	(1,1,1)     3	(2,1,1)     2	(1,2,1)     3	(2,2,1)     2	(2,1,2)     3	(1,2,2)     4	(2,2,2)     4</pre><pre class="codeinput">+A <span class="comment">%&lt;-- Calls uplus.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)     1	(2,1,1)     2	(1,2,1)     3	(2,2,1)     4	(1,1,2)     1	(2,1,2)     2	(1,2,2)     2	(2,2,2)     2</pre><pre class="codeinput">-A <span class="comment">%&lt;-- Calls uminus.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)    -1	(2,1,1)    -2	(1,2,1)    -3	(2,2,1)    -4	(1,1,2)    -1	(2,1,2)    -2	(1,2,2)    -2	(2,2,2)    -2</pre><pre class="codeinput">A+B <span class="comment">%&lt;-- Calls plus.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)     4	(1,1,2)     1	(1,2,1)     6	(1,2,2)     6	(2,1,1)     4	(2,1,2)     5	(2,2,1)     6	(2,2,2)     6</pre><pre class="codeinput">A-B <span class="comment">%&lt;-- Calls minus.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 6 nonzeros	(1,1,1)    -2	(1,1,2)     1	(1,2,2)    -2	(2,1,2)    -1	(2,2,1)     2

⌨️ 快捷键说明

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