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

📄 a2_sptensor_doc.html

📁 张量分析工具
💻 HTML
📖 第 1 页 / 共 3 页
字号:
	(2,2,2)    -2</pre><pre class="codeinput">A.*B <span class="comment">%&lt;-- Calls times.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 7 nonzeros	(1,1,1)     3	(1,2,1)     9	(1,2,2)     8	(2,1,1)     4	(2,1,2)     6	(2,2,1)     8	(2,2,2)     8</pre><pre class="codeinput">5*A <span class="comment">%&lt;-- Calls mtimes.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)     5	(1,1,2)     5	(1,2,1)    15	(1,2,2)    10	(2,1,1)    10	(2,1,2)    10	(2,2,1)    20	(2,2,2)    10</pre><pre class="codeinput">A./2 <span class="comment">%&lt;-- Calls rdivide.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)    0.5000	(1,1,2)    0.5000	(1,2,1)    1.5000	(1,2,2)    1.0000	(2,1,1)    1.0000	(2,1,2)    1.0000	(2,2,1)    2.0000	(2,2,2)    1.0000</pre><p>Elementwise divsion by another sptensor is allowed, but if the sparsity pattern of the denominator should be a superset of            the numerator.         </p><pre class="codeinput">A./(A+B) <span class="comment">%&lt;-- Calls rdivide.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)    0.2500	(1,1,2)    1.0000	(1,2,1)    0.5000	(1,2,2)    0.3333	(2,1,1)    0.5000	(2,1,2)    0.4000	(2,2,1)    0.6667	(2,2,2)    0.3333</pre><pre class="codeinput">A./B <span class="comment">%&lt;-- Uh-oh. Getting a divide by zero.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 2 x 2 x 2 with 8 nonzeros	(1,1,1)    0.3333	(1,1,2)       Inf	(1,2,1)    1.0000	(1,2,2)    0.5000	(2,1,1)    1.0000	(2,1,2)    0.6667	(2,2,1)    2.0000	(2,2,2)    0.5000</pre><h2>Use permute to reorder the modes of a sptensor<a name="59"></a></h2><pre class="codeinput">A = sptenrand([30 40 20 1], 5) <span class="comment">%&lt;-- Create data.</span></pre><pre class="codeoutput">A is a sparse tensor of size 30 x 40 x 20 x 1 with 5 nonzeros	( 4,33, 8,1)    0.7505	(11,40, 6,1)    0.7400	(15,23, 2,1)    0.4319	(20,27,11,1)    0.6343	(22, 6,20,1)    0.8030</pre><pre class="codeinput">permute(A,[4 3 2 1]) <span class="comment">%&lt;-- Reorder the modes.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 1 x 20 x 40 x 30 with 5 nonzeros	(1, 8,33, 4)    0.7505	(1, 6,40,11)    0.7400	(1, 2,23,15)    0.4319	(1,11,27,20)    0.6343	(1,20, 6,22)    0.8030</pre><p>Permute works correctly for a 1-dimensional sptensor.</p><pre class="codeinput">X = sptenrand(40,4) <span class="comment">%&lt;-- Create data.</span></pre><pre class="codeoutput">X is a sparse tensor of size 40 with 4 nonzeros	( 4)    0.2536	(25)    0.8735	(37)    0.5134	(38)    0.7327</pre><pre class="codeinput">permute(X,1) <span class="comment">%&lt;-- Permute.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 40 with 4 nonzeros	( 4)    0.2536	(25)    0.8735	(37)    0.5134	(38)    0.7327</pre><h2>Displaying a tensor<a name="63"></a></h2>         <p>The function <tt>disp</tt> handles small and large elements appropriately, as well as aligning the indices.         </p><pre class="codeinput">X = sptensor([1 1 1]); <span class="comment">%&lt;-- Create an empty sptensor.</span>X(1,1,1) = rand(1)*1e15; <span class="comment">%&lt;-- Insert a very big element.</span>X(4,3,2) = rand(1)*1e-15; <span class="comment">%&lt;-- Insert a very small element.</span>X(2,2,2) = rand(1); <span class="comment">%&lt;-- Insert a 'normal' element.</span>disp(X)</pre><pre class="codeoutput">ans is a sparse tensor of size 4 x 3 x 2 with 3 nonzeros  1.0e+014 *	(1,1,1)    4.2223	(4,3,2)    0.0000	(2,2,2)    0.0000</pre><p class="footer"><br>            Published with MATLAB&reg; 7.2<br></p>      </div>      <!--##### SOURCE BEGIN #####%% Sparse Tensors
% 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 |sptensor| class stores the data in coordinate
% format.
%% Creating a sptensor 
% 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.
rand('state',0); %<REPLACE_WITH_DASH_DASH Setup for the script
subs = [1,1,1;1,2,1;3,4,2]; %<REPLACE_WITH_DASH_DASH Subscripts of the nonzeros.
vals = [1; 2; 3]; %<REPLACE_WITH_DASH_DASH The values of the nonzeros.
X = sptensor(subs,vals) %<REPLACE_WITH_DASH_DASH Create a sparse tensor with 3 nonzeros.
%%
X = sptensor(subs,vals,[3 5 2]) %<REPLACE_WITH_DASH_DASH Or, specify the size explicitly.
%%
% Values corresponding to repeated subscripts are summed. Also note that we
% can use a scalar as the second argument.
subs = [1 1 1; 1 1 3; 2 2 2; 4 4 4; 1 1 1; 1 1 1]; %<REPLACE_WITH_DASH_DASH (1,1,1) is repeated.
X = sptensor(subs,2) %<REPLACE_WITH_DASH_DASH Equivalent to X = sptensor(subs,2*ones(6,1)).
%% Specifying the accumulation method for the constructor
% By default, values corresponding to repeated elements are summed.
% However, it is possible to specify other actions to be taken.
X = sptensor(subs,2*ones(6,1),[4 4 4],@max) %<REPLACE_WITH_DASH_DASH Maximum element.
%%
myfun = @(x) sum(x) / 3; %<REPLACE_WITH_DASH_DASH Total sum divided by three.
X = sptensor(subs,2*ones(6,1),[4 4 4],myfun) %<REPLACE_WITH_DASH_DASH Custom accumulation function.
%% Creating a one-dimensional sptensor.
X = sptensor([1;3;5],1,10) %<REPLACE_WITH_DASH_DASH Same as X = sptensor([1;3;5],[1;1;1],1,10).
%%
X = sptenrand(50,5) %<REPLACE_WITH_DASH_DASH A random, sparse, order-1 tensor with 5 nonzeros.
%% Creating an all-zero sptensor 
X = sptensor([],[],[10 10 10]) %<REPLACE_WITH_DASH_DASH Creates an all-zero tensor.
%%
X = sptensor([10 10 10]) %<REPLACE_WITH_DASH_DASH Same as above.
%% Constituent parts of a sptensor
X = sptenrand([40 30 20],5); %<REPLACE_WITH_DASH_DASH Create data.
X.subs %<REPLACE_WITH_DASH_DASH Subscripts of nonzeros.
%%
X.vals %<REPLACE_WITH_DASH_DASH Corresponding nonzero values.
%%
X.size %<REPLACE_WITH_DASH_DASH The size.
%% Creating a sparse tensor from its constituent parts
Y = sptensor(X.subs,X.vals,X.size) %<REPLACE_WITH_DASH_DASH Copies X.
%% Creating an empty sptensor
% An empty constructor exists, primarily to support loads of previously 
% saved data.
Y = sptensor %<REPLACE_WITH_DASH_DASH Create an empty sptensor.
%% Use sptenrand to create a random sptensor
X = sptenrand([10 10 10],0.01) %<REPLACE_WITH_DASH_DASH Create a tensor with 1% nonzeroes.
%%
% It is also posible to specify the precise number of nonzeros rather than
% a percentage.
X = sptenrand([10 10 10],10) %<REPLACE_WITH_DASH_DASH Create a tensor with 10 nonzeros.
%% Use squeeze to remove singleton dimensions from a sptensor
Y = sptensor([1 1 1; 2 1 1], 1, [2 1 1]) %<REPLACE_WITH_DASH_DASH Create a sparse tensor.
squeeze(Y) %<REPLACE_WITH_DASH_DASH Remove singleton dimensions.
%% Use full or tensor to convert a sptensor to a (dense) tensor
X = sptensor([1 1 1; 2 2 2], [1; 1]); %<REPLACE_WITH_DASH_DASH Create a sparse tensor.
Y = full(X) %<REPLACE_WITH_DASH_DASH Convert it to a (dense) tensor.
%%
Y = tensor(X) %<REPLACE_WITH_DASH_DASH Same as above.
%% Use sptensor to convert a (dense) tensor to a sptensor
Z = sptensor(Y) %<REPLACE_WITH_DASH_DASH Convert a tensor to a sptensor.
%% Use double to convert a sptensor to a (dense) multidimensional array
Y = double(X) %<REPLACE_WITH_DASH_DASH Creates a MATLAB array.
%% Use find to extract nonzeros from a tensor and then create a sptensor
% The |find| command can be used to extract specific elements and then
% convert those into a sptensor.
X = tensor(rand(5,4,2),[5 4 2]) %<REPLACE_WITH_DASH_DASH Create a tensor.
S = find(X > 0.9) %<REPLACE_WITH_DASH_DASH Extract subscipts of values greater than 0.9.
V = X(S) %<REPLACE_WITH_DASH_DASH Extract the corresponding values.
Y = sptensor(S,V,[5 4 2]) %<REPLACE_WITH_DASH_DASH Create a new tensor.
%% Use ndims and size to get the size of a sptensor
ndims(Y) %<REPLACE_WITH_DASH_DASH Number of dimensions or modes.
%%
size(Y) %<REPLACE_WITH_DASH_DASH Size of Y.
%% 
size(Y,3) %<REPLACE_WITH_DASH_DASH Size of mode 3 of Y.
%% Use nnz to get the number of nonzeros of a sptensor
nnz(Y) %<REPLACE_WITH_DASH_DASH Number of nonzeros in Y.
%% Subscripted reference for a sptensor
X = sptensor([4,4,4;2,2,1;2,3,2],[3;5;1],[4 4 4]) %<REPLACE_WITH_DASH_DASH Create a sptensor.
%% 
X(1,2,1) %<REPLACE_WITH_DASH_DASH Extract the (1,2,1) element, which is zero.
%%
X(4,4,4) %<REPLACE_WITH_DASH_DASH Extract the (4,4,4) element, which is nonzero.
%% 
X(1:2,2:4,:) %<REPLACE_WITH_DASH_DASH Extract a 2 x 3 x 4 subtensor.
%%
X([1 1 1; 2 2 1]) %<REPLACE_WITH_DASH_DASH Extract elements by subscript.
%%
X([1;6]) %<REPLACE_WITH_DASH_DASH Same as above but with linear indices.
%%
% As with a tensor, subscriped reference may be ambiguous for
% one-dimensional tensors. 
X = sptensor([1;3;5],1,7) %<REPLACE_WITH_DASH_DASH Create a sparse tensor.
%%
X(3) %<REPLACE_WITH_DASH_DASH Fully specified, single elements are always returned as scalars.
%%
X([3;6]) %<REPLACE_WITH_DASH_DASH Returns a subtensor.
%%
X([3;6],'extract') %<REPLACE_WITH_DASH_DASH Same as above *but* returns an array.
%% Subscripted assignment for a sptensor
X = sptensor([30 40 20]) %<REPLACE_WITH_DASH_DASH Create an emtpy 30 x 40 x 20 sptensor.
%% 
X(30,40,20) = 7 %<REPLACE_WITH_DASH_DASH Assign a single element.
%%
X([1,1,1;2,2,2]) = [1;1] %<REPLACE_WITH_DASH_DASH Assign a list of elements.
%%
X(11:20,11:20,11:20) = sptenrand([10,10,10],10) %<REPLACE_WITH_DASH_DASH Assign a subtensor.
%%
X(31,41,21) = 4 %<REPLACE_WITH_DASH_DASH Grows the size of the sptensor.
%%
X(111:120,111:120,111:120) = sptenrand([10,10,10],10) %<REPLACE_WITH_DASH_DASH Grow more.
%% Use end as the last index.
X(end-10:end,end-10:end,end-5:end)  %<REPLACE_WITH_DASH_DASH Same as X(108:118,110:120,115:120)
%% Use elemfun to manipulate the nonzeros of a sptensor
% The function |elemfun| is similar to |spfun| for sparse matrices.
X = sptenrand([10,10,10],3) %<REPLACE_WITH_DASH_DASH Create some data.
%%
Z = elemfun(X, @sqrt) %<REPLACE_WITH_DASH_DASH Square root of every nonzero.
%%
Z = elemfun(X, @(x) x+1) %<REPLACE_WITH_DASH_DASH Use a custom function.
%%
Z = elemfun(X, @(x) x~=0) %<REPLACE_WITH_DASH_DASH Set every nonzero to one.
%%
Z = ones(X) %<REPLACE_WITH_DASH_DASH An easier way to change every nonzero to one.
%% Basic operations (plus, minus, times, etc.) on a sptensor
A = sptensor(tensor(floor(5*rand(2,2,2)))) %<REPLACE_WITH_DASH_DASH Create data.
B = sptensor(tensor(floor(5*rand(2,2,2)))) %<REPLACE_WITH_DASH_DASH Create more data.
%%
+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./2 %<REPLACE_WITH_DASH_DASH Calls rdivide.
%%
% Elementwise divsion by another sptensor is allowed, but 
% if the sparsity pattern of the denominator should be a
% superset of the numerator.
A./(A+B) %<REPLACE_WITH_DASH_DASH Calls rdivide.
%%
A./B %<REPLACE_WITH_DASH_DASH Uh-oh. Getting a divide by zero.
%% Use permute to reorder the modes of a sptensor
A = sptenrand([30 40 20 1], 5) %<REPLACE_WITH_DASH_DASH Create data.
%%
permute(A,[4 3 2 1]) %<REPLACE_WITH_DASH_DASH Reorder the modes.
%%
% Permute works correctly for a 1-dimensional sptensor.
X = sptenrand(40,4) %<REPLACE_WITH_DASH_DASH Create data.
%%
permute(X,1) %<REPLACE_WITH_DASH_DASH Permute.
%% Displaying a tensor
% The function |disp| handles small and large elements appropriately, as
% well as aligning the indices.
X = sptensor([1 1 1]); %<REPLACE_WITH_DASH_DASH Create an empty sptensor. 
X(1,1,1) = rand(1)*1e15; %<REPLACE_WITH_DASH_DASH Insert a very big element.
X(4,3,2) = rand(1)*1e-15; %<REPLACE_WITH_DASH_DASH Insert a very small element.
X(2,2,2) = rand(1); %<REPLACE_WITH_DASH_DASH Insert a 'normal' element.
disp(X)
##### SOURCE END #####-->   </body></html>

⌨️ 快捷键说明

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