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

📄 a1_tensor_doc.html

📁 张量分析工具
💻 HTML
📖 第 1 页 / 共 3 页
字号:
            dimensions are handled explicitly.         </p><pre class="codeinput">X(1:2,[2 4],1,:) <span class="comment">%&lt;-- Produces a tensor of size 2 x 2 x 1.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 2 x 1	ans(:,:,1) = 	    0.0294    0.6226	    0.3576    0.1326</pre><p>It's also possible to extract a list of elements by passing in an array of subscripts or a column array of linear indices.</p><pre class="codeinput">subs = [1,1,1,1; 3,4,2,1]; X(subs) <span class="comment">%&lt;-- Extract 2 values by subscript.</span></pre><pre class="codeoutput">ans =    0.1557    0.8445</pre><pre class="codeinput">inds = [1; 24]; X(inds) <span class="comment">%&lt;-- Same thing with linear indices.</span></pre><pre class="codeoutput">ans =    0.1557    0.8445</pre><p>The difference between extracting a subtensor and a list of linear indices is ambiguous for 1-dimensional tensors. We can            specify 'extract' as a second argument whenever we are using a list of subscripts.         </p><pre class="codeinput">X = tenrand(10); <span class="comment">%&lt;-- Create a random tensor.</span>X([1:6]') <span class="comment">%&lt;-- Extract a subtensor.</span></pre><pre class="codeoutput">ans is a tensor of size 6	ans(:) = 	    0.8792	    0.1870	    0.9913	    0.7120	    0.8714	    0.4796</pre><pre class="codeinput">X([1:6]',<span class="string">'extract'</span>) <span class="comment">%&lt;-- Same thing *but* result is a vector.</span></pre><pre class="codeoutput">ans =    0.8792    0.1870    0.9913    0.7120    0.8714    0.4796</pre><h2>Subscripted assignment for a tensor<a name="29"></a></h2>         <p>We can assign a single element, an entire subtensor, or a list of values for a tensor.</p><pre class="codeinput">X = tenrand([3,4,2]); <span class="comment">%&lt;-- Create some data.</span>X(1,1,1) = 0 <span class="comment">%&lt;-- Replaces the (1,1,1) element.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2	X(:,:,1) = 	         0    0.0134    0.8893    0.3167	    0.9171    0.3697    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    0.3969    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776</pre><pre class="codeinput">X(1:2,1:2,1) = ones(2,2) <span class="comment">%&lt;-- Replaces a 2 x 2 subtensor.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2	X(:,:,1) = 	    1.0000    1.0000    0.8893    0.3167	    1.0000    1.0000    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    0.3969    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776</pre><pre class="codeinput">X([1 1 1;1 1 2]) = [5;7] <span class="comment">%&lt;-- Replaces the (1,1,1) and (1,1,2) elements.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2	X(:,:,1) = 	    5.0000    1.0000    0.8893    0.3167	    1.0000    1.0000    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    7.0000    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776</pre><pre class="codeinput">X([1;13]) = [5;7] <span class="comment">%&lt;-- Same as above using linear indices.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2	X(:,:,1) = 	    5.0000    1.0000    0.8893    0.3167	    1.0000    1.0000    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    7.0000    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776</pre><p>It is possible to <b>grow</b> the tensor automatically by assigning elements outside the original range of the tensor.         </p><pre class="codeinput">X(1,1,3) = 1 <span class="comment">%&lt;-- Grows the size of the tensor.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 3	X(:,:,1) = 	    5.0000    1.0000    0.8893    0.3167	    1.0000    1.0000    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    7.0000    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776	X(:,:,3) = 	     1     0     0     0	     0     0     0     0	     0     0     0     0</pre><h2>Using end for the last array index.<a name="34"></a></h2><pre class="codeinput">X(end,end,end)  <span class="comment">%&lt;-- Same as X(3,4,3).</span></pre><pre class="codeoutput">ans =     0</pre><pre class="codeinput">X(1,1,1:end-1)  <span class="comment">%&lt;-- Same as X(1,1,1:2).</span></pre><pre class="codeoutput">ans is a tensor of size 2	ans(:) = 	     5	     7</pre><p>It is also possible to use <tt>end</tt> to index past the end of an array.         </p><pre class="codeinput">X(1,1,end+1) = 5 <span class="comment">%&lt;-- Same as X(1,1,4).</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 4	X(:,:,1) = 	    5.0000    1.0000    0.8893    0.3167	    1.0000    1.0000    0.5938    0.2334	    0.1233    0.6986    0.1567    0.0084	X(:,:,2) = 	    7.0000    0.7688    0.7820    0.2632	    0.6499    0.9697    0.2376    0.7138	    0.0850    0.7148    0.1957    0.9776	X(:,:,3) = 	     1     0     0     0	     0     0     0     0	     0     0     0     0	X(:,:,4) = 	     5     0     0     0	     0     0     0     0	     0     0     0     0</pre><h2>Use find for subscripts of nonzero elements of a tensor<a name="37"></a></h2>         <p>The <tt>find</tt> function returns a list of nonzero <b>subscripts</b> for a tensor. Note that differs from the standard version, which returns linear indices.         </p><pre class="codeinput">X = tensor(floor(3*rand(2,2,2))) <span class="comment">%&lt;-- Generate some data.</span></pre><pre class="codeoutput">X is a tensor of size 2 x 2 x 2	X(:,:,1) = 	     1     2	     1     2	X(:,:,2) = 	     2     2	     2     2</pre><pre class="codeinput">[S,V] = find(X) <span class="comment">%&lt;-- Find all the nonzero subscripts and values.</span></pre><pre class="codeoutput">S =     1     1     1     2     1     1     1     2     1     2     2     1     1     1     2     2     1     2     1     2     2     2     2     2V =     1     1     2     2     2     2     2     2</pre><pre class="codeinput">S = find(X &gt;= 2) <span class="comment">%&lt;-- Find subscripts of values &gt;= 2.</span></pre><pre class="codeoutput">S =     1     2     1     2     2     1     1     1     2     2     1     2     1     2     2     2     2     2</pre><pre class="codeinput">V = X(S) <span class="comment">%&lt;-- Extract the corresponding values from X.</span></pre><pre class="codeoutput">V =     2     2     2     2     2     2</pre><h2>Basic operations (plus, minus, and, or, etc.) on a tensor<a name="41"></a></h2>         <p>The tensor object supports many basic operations, illustrated here.</p><pre class="codeinput">A = tensor(floor(3*rand(2,3,2)))B = tensor(floor(3*rand(2,3,2)))</pre><pre class="codeoutput">A is a tensor of size 2 x 3 x 2	A(:,:,1) = 	     1     1     1	     2     0     1	A(:,:,2) = 	     2     0     1	     1     2     0B is a tensor of size 2 x 3 x 2	B(:,:,1) = 	     2     2     0	     0     0     1	B(:,:,2) = 	     0     0     0	     1     0     1</pre><pre class="codeinput">A &amp; B <span class="comment">%&lt;-- Calls and.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     0	     0     0     1	ans(:,:,2) = 	     0     0     0	     1     0     0</pre><pre class="codeinput">A | B <span class="comment">%&lt;-- Calls or.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     1	     1     0     1	ans(:,:,2) = 	     1     0     1	     1     1     1</pre><pre class="codeinput">xor(A,B) <span class="comment">%&lt;-- Calls xor.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     0     0     1	     1     0     0	ans(:,:,2) = 	     1     0     1	     0     1     1</pre><pre class="codeinput">A==B <span class="comment">%&lt;-- Calls eq.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     0     0     0	     0     1     1	ans(:,:,2) = 	     0     1     0	     1     0     0</pre><pre class="codeinput">A~=B <span class="comment">%&lt;-- Calls neq.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     1	     1     0     0	ans(:,:,2) = 	     1     0     1	     0     1     1</pre><pre class="codeinput">A&gt;B <span class="comment">%&lt;-- Calls gt.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     0     0     1	     1     0     0	ans(:,:,2) = 	     1     0     1	     0     1     0</pre><pre class="codeinput">A&gt;=B <span class="comment">%&lt;-- Calls ge.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     0     0     1	     1     1     1	ans(:,:,2) = 	     1     1     1	     1     1     0</pre><pre class="codeinput">A&lt;B <span class="comment">%&lt;-- Calls lt.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     0	     0     0     0	ans(:,:,2) = 	     0     0     0	     0     0     1</pre><pre class="codeinput">A&lt;=B <span class="comment">%&lt;-- Calls le.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     0	     0     1     1	ans(:,:,2) = 	     0     1     0	     1     0     1</pre><pre class="codeinput">~A <span class="comment">%&lt;-- Calls not.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     0     0     0	     0     1     0	ans(:,:,2) = 	     0     1     0	     0     0     1</pre><pre class="codeinput">+A <span class="comment">%&lt;-- Calls uplus.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     1     1     1	     2     0     1	ans(:,:,2) = 	     2     0     1	     1     2     0</pre><pre class="codeinput">-A <span class="comment">%&lt;-- Calls uminus.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	    -1    -1    -1	    -2     0    -1	ans(:,:,2) = 	    -2     0    -1	    -1    -2     0</pre><pre class="codeinput">A+B <span class="comment">%&lt;-- Calls plus.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     3     3     1	     2     0     2	ans(:,:,2) = 	     2     0     1	     2     2     1</pre><pre class="codeinput">A-B <span class="comment">%&lt;-- Calls minus.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	    -1    -1     1	     2     0     0	ans(:,:,2) = 	     2     0     1	     0     2    -1</pre><pre class="codeinput">A.*B <span class="comment">%&lt;-- Calls times.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     2     2     0	     0     0     1	ans(:,:,2) = 	     0     0     0	     1     0     0</pre><pre class="codeinput">5*A <span class="comment">%&lt;-- Calls mtimes.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2	ans(:,:,1) = 	     5     5     5	    10     0     5

⌨️ 快捷键说明

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