📄 a1_tensor_doc.html
字号:
ans(:,:,2) = 10 0 5 5 10 0</pre><pre class="codeinput">A.^B <span class="comment">%<-- Calls power.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 1 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 1 0</pre><pre class="codeinput">A.^2 <span class="comment">%<-- Calls power.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 1 1 1 4 0 1 ans(:,:,2) = 4 0 1 1 4 0</pre><pre class="codeinput">A.\B <span class="comment">%<-- Calls ldivide.</span></pre><pre class="codeoutput">Warning: Divide by zero.ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 2 2 0 0 NaN 1 ans(:,:,2) = 0 NaN 0 1 0 Inf</pre><pre class="codeinput">A./2 <span class="comment">%<-- Calls rdivide.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 0.5000 0.5000 0.5000 1.0000 0 0.5000 ans(:,:,2) = 1.0000 0 0.5000 0.5000 1.0000 0</pre><pre class="codeinput">A./B <span class="comment">%<-- Calls rdivide (but beware divides by zero!)</span></pre><pre class="codeoutput">Warning: Divide by zero.ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 0.5000 0.5000 Inf Inf NaN 1.0000 ans(:,:,2) = Inf NaN Inf 1 Inf 0</pre><h2>Using tenfun for elementwise operations on one or more tensors<a name="63"></a></h2> <p>The function <tt>tenfun</tt> applies a specified function to a number of tensors. This can be used for any function that is not predefined for tensors. </p><pre class="codeinput">tenfun(@(x)+1,A) <span class="comment">%<-- Increment every element of A by one.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 1 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 1 1</pre><pre class="codeinput">tenfun(@max,A,B) <span class="comment">%<-- Max of A and B, elementwise.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 2 2 1 2 0 1 ans(:,:,2) = 2 0 1 1 2 1</pre><pre class="codeinput">C = tensor(floor(5*rand(2,3,2))) <span class="comment">%<-- Create another tensor.</span>tenfun(@median,A,B,C) <span class="comment">%<-- Elementwise means for A, B, and C.</span></pre><pre class="codeoutput">C is a tensor of size 2 x 3 x 2 C(:,:,1) = 4 0 4 4 3 3 C(:,:,2) = 4 3 4 3 1 1ans is a tensor of size 2 x 3 x 2 ans(:,:,1) = 2 1 1 2 0 1 ans(:,:,2) = 2 0 1 1 1 1</pre><h2>Use permute to reorder the modes of a tensor<a name="66"></a></h2><pre class="codeinput">X = tensor(1:24,[3 4 2]) <span class="comment">%<-- Create a tensor.</span></pre><pre class="codeoutput">X is a tensor of size 3 x 4 x 2 X(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 X(:,:,2) = 13 16 19 22 14 17 20 23 15 18 21 24</pre><pre class="codeinput">permute(X,[3 2 1]) <span class="comment">%<-- Reverse the modes.</span></pre><pre class="codeoutput">ans is a tensor of size 2 x 4 x 3 ans(:,:,1) = 1 4 7 10 13 16 19 22 ans(:,:,2) = 2 5 8 11 14 17 20 23 ans(:,:,3) = 3 6 9 12 15 18 21 24</pre><p>Permuting a 1-dimensional tensor works correctly.</p><pre class="codeinput">X = tensor(1:4,4); <span class="comment">%<-- Create a 1-way tensor.</span>permute(X,1) <span class="comment">%<-- Call permute with *only* one dimension.</span></pre><pre class="codeoutput">ans is a tensor of size 4 ans(:) = 1 2 3 4</pre><h2>Displaying a tensor<a name="69"></a></h2> <p>The function <tt>disp</tt> can be used to display a tensor and correctly displays very small and large elements. </p><pre class="codeinput">X = tensor(1:24,[3 4 2]); <span class="comment">%<-- Create a 3 x 4 x 2 tensor.</span>X(:,:,1) = X(:,:,1) * 1e15; <span class="comment">%<-- Make the first slice very large.</span>X(:,:,2) = X(:,:,2) * 1e-15; <span class="comment">%<-- Make the second slice very small.</span>disp(X)</pre><pre class="codeoutput">ans is a tensor of size 3 x 4 x 2 ans(:,:,1) = 1.0e+016 * 0.1000 0.4000 0.7000 1.0000 0.2000 0.5000 0.8000 1.1000 0.3000 0.6000 0.9000 1.2000 ans(:,:,2) = 1.0e-013 * 0.1300 0.1600 0.1900 0.2200 0.1400 0.1700 0.2000 0.2300 0.1500 0.1800 0.2100 0.2400</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Tensors
% Tensors are extensions of multidimensional arrays with additional
% operations defined on them. Here we explain the basics of creating and
% working with tensors.
%% Creating a tensor from an array
% The |tensor| command converts a (multidimensional) array to a tensor
% object.
M = ones(4,3,2); %<REPLACE_WITH_DASH_DASH A 4 x 3 x 2 array.
X = tensor(M) %<REPLACE_WITH_DASH_DASH Convert to a tensor object.
%%
% Optionally, you can specify a different shape for the tensor, so long as
% the input array has the right number of elements.
X = tensor(M,[2 3 4]) %<REPLACE_WITH_DASH_DASH M has 24 elements.
%% Creating a one-dimensional tensor
% 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.
X = tensor(rand(5,1)) %<REPLACE_WITH_DASH_DASH Creates a 2-way tensor.
%%
% This is fixed by specifying the size explicitly.
X = tensor(rand(5,1),5) %<REPLACE_WITH_DASH_DASH Creates a 1-way tensor.
%% Specifying trailing singleton dimensions in a tensor
% Likewise, trailing singleton dimensions must be explictly specified.
Y = tensor(rand(4,3,1)) %<REPLACE_WITH_DASH_DASH Creates a 2-way tensor.
%%
Y = tensor(rand(4,3,1),[4 3 1]) %<REPLACE_WITH_DASH_DASH Creates a 3-way tensor.
%%
% Unfortunately, the |whos| command does not report the size of 1D
% objects correctly (last checked for MATLAB 2006a).
whos X Y %<REPLACE_WITH_DASH_DASH Doesn't report the right size for X!
%% The constituent parts of a tensor
X = tenrand([4 3 2]); %<REPLACE_WITH_DASH_DASH Create data.
X.data %<REPLACE_WITH_DASH_DASH The array.
%%
X.size %<REPLACE_WITH_DASH_DASH The size.
%% Creating a tensor from its constituent parts
Y = tensor(X.data,X.size) %<REPLACE_WITH_DASH_DASH Copies X.
%% Creating an empty tensor
% An empty constructor exists, primarily to support loading previously
% saved data in MAT-files.
X = tensor %<REPLACE_WITH_DASH_DASH Creates an empty tensor.
%% Use tenone to create a tensor of all ones
X = tenones([3 4 2]) %<REPLACE_WITH_DASH_DASH Creates a 3 x 4 x 2 tensor of ones.
%% Use tenzeros to create a tensor of all zeros
X = tenzeros([1 4 2]) %<REPLACE_WITH_DASH_DASH Creates a 1 x 4 x 2 tensor of zeros.
%% Use tenrand to create a random tensor
X = tenrand([5 4 2]) %<REPLACE_WITH_DASH_DASH Creates a random 5 x 4 x 2 tensor.
%% Use squeeze to remove singleton dimensions from a tensor
squeeze(Y) %<REPLACE_WITH_DASH_DASH Removes singleton dimensions.
%% Use double to convert a tensor to a (multidimensional) array
double(Y) %<REPLACE_WITH_DASH_DASH Converts Y to a standard MATLAB array.
%%
Y.data %<REPLACE_WITH_DASH_DASH Same thing.
%% Use ndims and size to get the size of a tensor
ndims(Y) %<REPLACE_WITH_DASH_DASH Number of dimensions (or ways).
%%
size(Y) %<REPLACE_WITH_DASH_DASH Row vector with the sizes of all dimension.
%%
size(Y,3) %<REPLACE_WITH_DASH_DASH Size of a single dimension.
%% Subscripted reference for a tensor
X = tenrand([3 4 2 1]); %<REPLACE_WITH_DASH_DASH Create a 3 x 4 x 2 x 1 random tensor.
X(1,1,1,1) %<REPLACE_WITH_DASH_DASH Extract a single element.
%%
% It is possible to extract a subtensor that contains a single element.
% Observe that singleton dimensions are *not* dropped unless they are
% specifically specified, e.g., as above.
X(1,1,1,:) %<REPLACE_WITH_DASH_DASH Produces a tensor of order 1 and size 1.
%%
% In general, specified dimensions are dropped from the result. Here we
% specify the second and third dimension.
X(:,1,1,:) %<REPLACE_WITH_DASH_DASH Produces a tensor of size 3 x 1.
%%
% Moreover, the subtensor is automatically renumbered/resized in the same
% way that MATLAB works for arrays except that singleton dimensions are
% handled explicitly.
X(1:2,[2 4],1,:) %<REPLACE_WITH_DASH_DASH Produces a tensor of size 2 x 2 x 1.
%%
% It's also possible to extract a list of elements by passing in an array
% of subscripts or a column array of linear indices.
subs = [1,1,1,1; 3,4,2,1]; X(subs) %<REPLACE_WITH_DASH_DASH Extract 2 values by subscript.
%%
inds = [1; 24]; X(inds) %<REPLACE_WITH_DASH_DASH Same thing with linear indices.
%%
% 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.
X = tenrand(10); %<REPLACE_WITH_DASH_DASH Create a random tensor.
X([1:6]') %<REPLACE_WITH_DASH_DASH Extract a subtensor.
%%
X([1:6]','extract') %<REPLACE_WITH_DASH_DASH Same thing *but* result is a vector.
%% Subscripted assignment for a tensor
% We can assign a single element, an entire subtensor, or a list of values
% for a tensor.
X = tenrand([3,4,2]); %<REPLACE_WITH_DASH_DASH Create some data.
X(1,1,1) = 0 %<REPLACE_WITH_DASH_DASH Replaces the (1,1,1) element.
%%
X(1:2,1:2,1) = ones(2,2) %<REPLACE_WITH_DASH_DASH Replaces a 2 x 2 subtensor.
%%
X([1 1 1;1 1 2]) = [5;7] %<REPLACE_WITH_DASH_DASH Replaces the (1,1,1) and (1,1,2) elements.
%%
X([1;13]) = [5;7] %<REPLACE_WITH_DASH_DASH Same as above using linear indices.
%%
% It is possible to *grow* the tensor automatically by assigning elements
% outside the original range of the tensor.
X(1,1,3) = 1 %<REPLACE_WITH_DASH_DASH Grows the size of the tensor.
%% Using end for the last array index.
X(end,end,end) %<REPLACE_WITH_DASH_DASH Same as X(3,4,3).
%%
X(1,1,1:end-1) %<REPLACE_WITH_DASH_DASH Same as X(1,1,1:2).
%%
% It is also possible to use |end| to index past the end of an array.
X(1,1,end+1) = 5 %<REPLACE_WITH_DASH_DASH Same as X(1,1,4).
%% Use find for subscripts of nonzero elements of a tensor
% The |find| function returns a list of nonzero *subscripts* for a tensor.
% Note that differs from the standard version, which returns linear
% indices.
X = tensor(floor(3*rand(2,2,2))) %<REPLACE_WITH_DASH_DASH Generate some data.
%%
[S,V] = find(X) %<REPLACE_WITH_DASH_DASH Find all the nonzero subscripts and values.
%%
S = find(X >= 2) %<REPLACE_WITH_DASH_DASH Find subscripts of values >= 2.
%%
V = X(S) %<REPLACE_WITH_DASH_DASH Extract the corresponding values from X.
%% Basic operations (plus, minus, and, or, etc.) on a tensor
% The tensor object supports many basic operations, illustrated here.
A = tensor(floor(3*rand(2,3,2)))
B = tensor(floor(3*rand(2,3,2)))
%%
A & B %<REPLACE_WITH_DASH_DASH Calls and.
%%
A | B %<REPLACE_WITH_DASH_DASH Calls or.
%%
xor(A,B) %<REPLACE_WITH_DASH_DASH Calls xor.
%%
A==B %<REPLACE_WITH_DASH_DASH Calls eq.
%%
A~=B %<REPLACE_WITH_DASH_DASH Calls neq.
%%
A>B %<REPLACE_WITH_DASH_DASH Calls gt.
%%
A>=B %<REPLACE_WITH_DASH_DASH Calls ge.
%%
A<B %<REPLACE_WITH_DASH_DASH Calls lt.
%%
A<=B %<REPLACE_WITH_DASH_DASH Calls le.
%%
~A %<REPLACE_WITH_DASH_DASH Calls not.
%%
+A %<REPLACE_WITH_DASH_DASH Calls uplus.
%%
-A %<REPLACE_WITH_DASH_DASH Calls uminus.
%%
A+B %<REPLACE_WITH_DASH_DASH Calls plus.
%%
A-B %<REPLACE_WITH_DASH_DASH Calls minus.
%%
A.*B %<REPLACE_WITH_DASH_DASH Calls times.
%%
5*A %<REPLACE_WITH_DASH_DASH Calls mtimes.
%%
A.^B %<REPLACE_WITH_DASH_DASH Calls power.
%%
A.^2 %<REPLACE_WITH_DASH_DASH Calls power.
%%
A.\B %<REPLACE_WITH_DASH_DASH Calls ldivide.
%%
A./2 %<REPLACE_WITH_DASH_DASH Calls rdivide.
%%
A./B %<REPLACE_WITH_DASH_DASH Calls rdivide (but beware divides by zero!)
%% Using tenfun for elementwise operations on one or more tensors
% The function |tenfun| applies a specified function to a number of
% tensors. This can be used for any function that is not predefined for
% tensors.
tenfun(@(x)+1,A) %<REPLACE_WITH_DASH_DASH Increment every element of A by one.
%%
tenfun(@max,A,B) %<REPLACE_WITH_DASH_DASH Max of A and B, elementwise.
%%
C = tensor(floor(5*rand(2,3,2))) %<REPLACE_WITH_DASH_DASH Create another tensor.
tenfun(@median,A,B,C) %<REPLACE_WITH_DASH_DASH Elementwise means for A, B, and C.
%% Use permute to reorder the modes of a tensor
X = tensor(1:24,[3 4 2]) %<REPLACE_WITH_DASH_DASH Create a tensor.
%%
permute(X,[3 2 1]) %<REPLACE_WITH_DASH_DASH Reverse the modes.
%%
% Permuting a 1-dimensional tensor works correctly.
X = tensor(1:4,4); %<REPLACE_WITH_DASH_DASH Create a 1-way tensor.
permute(X,1) %<REPLACE_WITH_DASH_DASH Call permute with *only* one dimension.
%% Displaying a tensor
% The function |disp| can be used to display a tensor and correctly
% displays very small and large elements.
X = tensor(1:24,[3 4 2]); %<REPLACE_WITH_DASH_DASH Create a 3 x 4 x 2 tensor.
X(:,:,1) = X(:,:,1) * 1e15; %<REPLACE_WITH_DASH_DASH Make the first slice very large.
X(:,:,2) = X(:,:,2) * 1e-15; %<REPLACE_WITH_DASH_DASH Make the second slice very small.
disp(X)
##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -