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

📄 a1_tensor_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>Tensors</title>      <meta name="generator" content="MATLAB 7.2">      <meta name="date" content="2007-01-10">      <meta name="m-file" content="A1_tensor_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>Tensors</h1>         <introduction>            <p>Tensors are extensions of multidimensional arrays with additional operations defined on them. Here we explain the basics of               creating and working with tensors.            </p>         </introduction>         <h2>Contents</h2>         <div>            <ul>               <li><a href="#1">Creating a tensor from an array</a></li>               <li><a href="#3">Creating a one-dimensional tensor</a></li>               <li><a href="#5">Specifying trailing singleton dimensions in a tensor</a></li>               <li><a href="#8">The constituent parts of a tensor</a></li>               <li><a href="#10">Creating a tensor from its constituent parts</a></li>               <li><a href="#11">Creating an empty tensor</a></li>               <li><a href="#12">Use tenone to create a tensor of all ones</a></li>               <li><a href="#13">Use tenzeros to create a tensor of all zeros</a></li>               <li><a href="#14">Use tenrand to create a random tensor</a></li>               <li><a href="#15">Use squeeze to remove singleton dimensions from a tensor</a></li>               <li><a href="#16">Use double to convert a tensor to a (multidimensional) array</a></li>               <li><a href="#18">Use ndims and size to get the size of a tensor</a></li>               <li><a href="#21">Subscripted reference for a tensor</a></li>               <li><a href="#29">Subscripted assignment for a tensor</a></li>               <li><a href="#34">Using end for the last array index.</a></li>               <li><a href="#37">Use find for subscripts of nonzero elements of a tensor</a></li>               <li><a href="#41">Basic operations (plus, minus, and, or, etc.) on a tensor</a></li>               <li><a href="#63">Using tenfun for elementwise operations on one or more tensors</a></li>               <li><a href="#66">Use permute to reorder the modes of a tensor</a></li>               <li><a href="#69">Displaying a tensor</a></li>            </ul>         </div>         <h2>Creating a tensor from an array<a name="1"></a></h2>         <p>The <tt>tensor</tt> command converts a (multidimensional) array to a tensor object.         </p><pre class="codeinput">M = ones(4,3,2); <span class="comment">%&lt;-- A 4 x 3 x 2 array.</span>X = tensor(M) <span class="comment">%&lt;-- Convert to a tensor object.</span></pre><pre class="codeoutput">X is a tensor of size 4 x 3 x 2	X(:,:,1) = 	     1     1     1	     1     1     1	     1     1     1	     1     1     1	X(:,:,2) = 	     1     1     1	     1     1     1	     1     1     1	     1     1     1</pre><p>Optionally, you can specify a different shape for the tensor, so long as the input array has the right number of elements.</p><pre class="codeinput">X = tensor(M,[2 3 4]) <span class="comment">%&lt;-- M has 24 elements.</span></pre><pre class="codeoutput">X is a tensor of size 2 x 3 x 4	X(:,:,1) = 	     1     1     1	     1     1     1	X(:,:,2) = 	     1     1     1	     1     1     1	X(:,:,3) = 	     1     1     1	     1     1     1	X(:,:,4) = 	     1     1     1	     1     1     1</pre><h2>Creating a one-dimensional tensor<a name="3"></a></h2>         <p>The tensor class explicitly supports order-one tensors as well as trailing singleton dimensions, but the size must be explicit            in the constructor. By default, a column array produces a 2-way tensor.         </p><pre class="codeinput">X = tensor(rand(5,1)) <span class="comment">%&lt;-- Creates a 2-way tensor.</span></pre><pre class="codeoutput">X is a tensor of size 5 x 1	X(:,:) = 	    0.9473	    0.8133	    0.9238	    0.1990	    0.6743</pre><p>This is fixed by specifying the size explicitly.</p><pre class="codeinput">X = tensor(rand(5,1),5) <span class="comment">%&lt;-- Creates a 1-way tensor.</span></pre><pre class="codeoutput">X is a tensor of size 5	X(:) = 	    0.9271	    0.3438	    0.5945	    0.6155	    0.0034</pre><h2>Specifying trailing singleton dimensions in a tensor<a name="5"></a></h2>         <p>Likewise, trailing singleton dimensions must be explictly specified.</p><pre class="codeinput">Y = tensor(rand(4,3,1)) <span class="comment">%&lt;-- Creates a 2-way tensor.</span></pre><pre class="codeoutput">Y is a tensor of size 4 x 3	Y(:,:) = 	    0.9820    0.7010    0.1121	    0.8995    0.6097    0.2916	    0.6928    0.2999    0.0974	    0.4397    0.8560    0.3974</pre><pre class="codeinput">Y = tensor(rand(4,3,1),[4 3 1]) <span class="comment">%&lt;-- Creates a 3-way tensor.</span></pre><pre class="codeoutput">Y is a tensor of size 4 x 3 x 1	Y(:,:,1) = 	    0.3333    0.0429    0.8068	    0.9442    0.0059    0.6376	    0.8386    0.5744    0.2513	    0.2584    0.7439    0.1443</pre><p>Unfortunately, the <tt>whos</tt> command does not report the size of 1D objects correctly (last checked for MATLAB 2006a).         </p><pre class="codeinput">whos <span class="string">X</span> <span class="string">Y</span> <span class="comment">%&lt;-- Doesn't report the right size for X!</span></pre><pre class="codeoutput">  Name      Size                           Bytes  Class  X         1x1                              296  tensor object  Y         4x3x1                            368  tensor objectGrand total is 25 elements using 664 bytes</pre><h2>The constituent parts of a tensor<a name="8"></a></h2><pre class="codeinput">X = tenrand([4 3 2]); <span class="comment">%&lt;-- Create data.</span>X.data <span class="comment">%&lt;-- The array.</span></pre><pre class="codeoutput">ans(:,:,1) =    0.6516    0.3099    0.2110    0.9461    0.2688    0.2168    0.8159    0.5365    0.6518    0.9302    0.1633    0.0528ans(:,:,2) =    0.2293    0.7207    0.1252    0.6674    0.9544    0.1662    0.3109    0.1311    0.9114    0.3066    0.0683    0.1363</pre><pre class="codeinput">X.size <span class="comment">%&lt;-- The size.</span></pre><pre class="codeoutput">ans =     4     3     2</pre><h2>Creating a tensor from its constituent parts<a name="10"></a></h2><pre class="codeinput">Y = tensor(X.data,X.size) <span class="comment">%&lt;-- Copies X.</span></pre><pre class="codeoutput">Y is a tensor of size 4 x 3 x 2	Y(:,:,1) = 	    0.6516    0.3099    0.2110	    0.9461    0.2688    0.2168	    0.8159    0.5365    0.6518	    0.9302    0.1633    0.0528	Y(:,:,2) = 	    0.2293    0.7207    0.1252	    0.6674    0.9544    0.1662	    0.3109    0.1311    0.9114	    0.3066    0.0683    0.1363</pre><h2>Creating an empty tensor<a name="11"></a></h2>         <p>An empty constructor exists, primarily to support loading previously saved data in MAT-files.</p><pre class="codeinput">X = tensor <span class="comment">%&lt;-- Creates an empty tensor.</span></pre><pre class="codeoutput">X is a tensor of size [empty tensor]	X = []</pre><h2>Use tenone to create a tensor of all ones<a name="12"></a></h2><pre class="codeinput">X = tenones([3 4 2]) <span class="comment">%&lt;-- Creates a 3 x 4 x 2 tensor of ones.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2	X(:,:,1) = 	     1     1     1     1	     1     1     1     1	     1     1     1     1	X(:,:,2) = 	     1     1     1     1	     1     1     1     1	     1     1     1     1</pre><h2>Use tenzeros to create a tensor of all zeros<a name="13"></a></h2><pre class="codeinput">X = tenzeros([1 4 2]) <span class="comment">%&lt;-- Creates a 1 x 4 x 2 tensor of zeros.</span></pre><pre class="codeoutput">X is a tensor of size 1 x 4 x 2	X(:,:,1) = 	     0     0     0     0	X(:,:,2) = 	     0     0     0     0</pre><h2>Use tenrand to create a random tensor<a name="14"></a></h2><pre class="codeinput">X = tenrand([5 4 2]) <span class="comment">%&lt;-- Creates a random 5 x 4 x 2 tensor.</span></pre><pre class="codeoutput">X is a tensor of size 5 x 4 x 2	X(:,:,1) = 	    0.6170    0.9413    0.8802    0.9562	    0.2690    0.3299    0.7496    0.1962	    0.2207    0.7045    0.3796    0.7762	    0.7129    0.9434    0.7256    0.6133	    0.5490    0.5816    0.1628    0.1623	X(:,:,2) = 	    0.0311    0.9585    0.2154    0.0178	    0.2886    0.6799    0.1824    0.8779	    0.9711    0.0550    0.0768    0.3525	    0.9505    0.5998    0.0074    0.7221	    0.2280    0.3931    0.7888    0.9685</pre><h2>Use squeeze to remove singleton dimensions from a tensor<a name="15"></a></h2><pre class="codeinput">squeeze(Y) <span class="comment">%&lt;-- Removes singleton dimensions.</span></pre><pre class="codeoutput">ans is a tensor of size 4 x 3 x 2	ans(:,:,1) = 	    0.6516    0.3099    0.2110	    0.9461    0.2688    0.2168	    0.8159    0.5365    0.6518	    0.9302    0.1633    0.0528	ans(:,:,2) = 	    0.2293    0.7207    0.1252	    0.6674    0.9544    0.1662	    0.3109    0.1311    0.9114	    0.3066    0.0683    0.1363</pre><h2>Use double to convert a tensor to a (multidimensional) array<a name="16"></a></h2><pre class="codeinput">double(Y) <span class="comment">%&lt;-- Converts Y to a standard MATLAB array.</span></pre><pre class="codeoutput">ans(:,:,1) =    0.6516    0.3099    0.2110    0.9461    0.2688    0.2168    0.8159    0.5365    0.6518    0.9302    0.1633    0.0528ans(:,:,2) =    0.2293    0.7207    0.1252    0.6674    0.9544    0.1662    0.3109    0.1311    0.9114    0.3066    0.0683    0.1363</pre><pre class="codeinput">Y.data <span class="comment">%&lt;-- Same thing.</span></pre><pre class="codeoutput">ans(:,:,1) =    0.6516    0.3099    0.2110    0.9461    0.2688    0.2168    0.8159    0.5365    0.6518    0.9302    0.1633    0.0528ans(:,:,2) =    0.2293    0.7207    0.1252    0.6674    0.9544    0.1662    0.3109    0.1311    0.9114    0.3066    0.0683    0.1363</pre><h2>Use ndims and size to get the size of a tensor<a name="18"></a></h2><pre class="codeinput">ndims(Y) <span class="comment">%&lt;-- Number of dimensions (or ways).</span></pre><pre class="codeoutput">ans =     3</pre><pre class="codeinput">size(Y) <span class="comment">%&lt;-- Row vector with the sizes of all dimension.</span></pre><pre class="codeoutput">ans =     4     3     2</pre><pre class="codeinput">size(Y,3) <span class="comment">%&lt;-- Size of a single dimension.</span></pre><pre class="codeoutput">ans =     2</pre><h2>Subscripted reference for a tensor<a name="21"></a></h2><pre class="codeinput">X = tenrand([3 4 2 1]); <span class="comment">%&lt;-- Create a 3 x 4 x 2 x 1 random tensor.</span>X(1,1,1,1) <span class="comment">%&lt;-- Extract a single element.</span></pre><pre class="codeoutput">ans =    0.1557</pre><p>It is possible to extract a subtensor that contains a single element. Observe that singleton dimensions are <b>not</b> dropped unless they are specifically specified, e.g., as above.         </p><pre class="codeinput">X(1,1,1,:) <span class="comment">%&lt;-- Produces a tensor of order 1 and size 1.</span></pre><pre class="codeoutput">ans is a tensor of size 1	ans(:) = 	    0.1557</pre><p>In general, specified dimensions are dropped from the result. Here we specify the second and third dimension.</p><pre class="codeinput">X(:,1,1,:) <span class="comment">%&lt;-- Produces a tensor of size 3 x 1.</span></pre><pre class="codeoutput">ans is a tensor of size 3 x 1	ans(:,:) = 	    0.1557	    0.1630	    0.3134</pre><p>Moreover, the subtensor is automatically renumbered/resized in the same way that MATLAB works for arrays except that singleton

⌨️ 快捷键说明

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