d_ktensor_doc.html
来自「张量分析工具」· HTML 代码 · 共 844 行 · 第 1/3 页
HTML
844 行
Y.core is a sparse tensor of size 2 x 2 x 2 with 2 nonzeros (1,1,1) 5.0000 (2,2,2) 0.2500 Y.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 Y.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 Y.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><pre class="codeinput">norm(full(X)-full(Y)) <span class="comment">%<-- They are the same.</span></pre><pre class="codeoutput">ans = 3.8057e-016</pre><h2>Use ndims and size for the dimensions of a ktensor<a name="16"></a></h2><pre class="codeinput">ndims(X) <span class="comment">%<-- Number of dimensions.</span></pre><pre class="codeoutput">ans = 3</pre><pre class="codeinput">size(X) <span class="comment">%<-- Row vector of the sizes.</span></pre><pre class="codeoutput">ans = 4 3 2</pre><pre class="codeinput">size(X,2) <span class="comment">%<-- Size of the 2nd mode.</span></pre><pre class="codeoutput">ans = 3</pre><h2>Subscripted reference for a ktensor<a name="19"></a></h2><pre class="codeinput">X(1,1,1) <span class="comment">%<-- Assemble the (1,1,1) element (requires computation).</span></pre><pre class="codeoutput">ans = 0.8529</pre><pre class="codeinput">X.lambda(2) <span class="comment">%<-- Weight of 2nd factor.</span></pre><pre class="codeoutput">ans = 0.2500</pre><pre class="codeinput">X.U{2} <span class="comment">%<-- Extract a matrix.</span></pre><pre class="codeoutput">ans = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382</pre><pre class="codeinput">X{2} <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">ans = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382</pre><h2>Subscripted assignment for a ktensor<a name="23"></a></h2><pre class="codeinput">X.lambda = ones(size(X.lambda)) <span class="comment">%<-- Insert new multipliers.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 x 2 X.lambda = [ 1 1 ] X.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 X.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 X.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><pre class="codeinput">X.lambda(1) = 7 <span class="comment">%<-- Change a single element of lambda.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 x 2 X.lambda = [ 7 1 ] X.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 X.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 X.U{3} = 0.1763 0.9355 0.4057 0.9169</pre><pre class="codeinput">X{3}(1:2,1) = [1;1] <span class="comment">%<-- Change the matrix for mode 3.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 x 2 X.lambda = [ 7 1 ] X.U{1} = 0.9501 0.8913 0.2311 0.7621 0.6068 0.4565 0.4860 0.0185 X.U{2} = 0.8214 0.7919 0.4447 0.9218 0.6154 0.7382 X.U{3} = 1.0000 0.9355 1.0000 0.9169</pre><h2>Use end for the last array index.<a name="26"></a></h2><pre class="codeinput">X(3:end,1,1) <span class="comment">%<-- Calculated X(3,1,1) and X((4,1,1).</span></pre><pre class="codeoutput">ans = 3.8274 2.8080</pre><pre class="codeinput">X(1,1,1:end-1) <span class="comment">%<-- Calculates X(1,1,1).</span></pre><pre class="codeoutput">ans = 6.1234</pre><pre class="codeinput">X{end} <span class="comment">%<-- Or use inside of curly braces. This is X{3}.</span></pre><pre class="codeoutput">ans = 1.0000 0.9355 1.0000 0.9169</pre><h2>Adding and subtracting ktensors<a name="29"></a></h2> <p>Adding two ktensors is the same as concatenating the matrices</p><pre class="codeinput">X = ktensor({rand(4,2),rand(2,2),rand(3,2)}) <span class="comment">%<-- Data.</span>Y = ktensor({rand(4,2),rand(2,2),rand(3,2)}) <span class="comment">%<-- More data.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 2 x 3 X.lambda = [ 1 1 ] X.U{1} = 0.4289 0.6822 0.3046 0.3028 0.1897 0.5417 0.1934 0.1509 X.U{2} = 0.6979 0.8600 0.3784 0.8537 X.U{3} = 0.5936 0.8216 0.4966 0.6449 0.8998 0.8180Y is a ktensor of size 4 x 2 x 3 Y.lambda = [ 1 1 ] Y.U{1} = 0.6602 0.5341 0.3420 0.7271 0.2897 0.3093 0.3412 0.8385 Y.U{2} = 0.5681 0.7027 0.3704 0.5466 Y.U{3} = 0.4449 0.7948 0.6946 0.9568 0.6213 0.5226</pre><pre class="codeinput">Z = X + Y <span class="comment">%<-- Concatenates the factor matrices.</span></pre><pre class="codeoutput">Z is a ktensor of size 4 x 2 x 3 Z.lambda = [ 1 1 1 1 ] Z.U{1} = 0.4289 0.6822 0.6602 0.5341 0.3046 0.3028 0.3420 0.7271 0.1897 0.5417 0.2897 0.3093 0.1934 0.1509 0.3412 0.8385 Z.U{2} = 0.6979 0.8600 0.5681 0.7027 0.3784 0.8537 0.3704 0.5466 Z.U{3} = 0.5936 0.8216 0.4449 0.7948 0.4966 0.6449 0.6946 0.9568 0.8998 0.8180 0.6213 0.5226</pre><pre class="codeinput">Z = X - Y <span class="comment">%<-- Concatenates as with plus, but changes the weights.</span></pre><pre class="codeoutput">Z is a ktensor of size 4 x 2 x 3 Z.lambda = [ 1 1 -1 -1 ] Z.U{1} = 0.4289 0.6822 0.6602 0.5341 0.3046 0.3028 0.3420 0.7271 0.1897 0.5417 0.2897 0.3093 0.1934 0.1509 0.3412 0.8385 Z.U{2} = 0.6979 0.8600 0.5681 0.7027 0.3784 0.8537 0.3704 0.5466 Z.U{3} = 0.5936 0.8216 0.4449 0.7948 0.4966 0.6449 0.6946 0.9568 0.8998 0.8180 0.6213 0.5226</pre><pre class="codeinput">norm( full(Z) - (full(X)-full(Y)) ) <span class="comment">%<-- Should be zero.</span></pre><pre class="codeoutput">ans = 1.7110e-016</pre><h2>Basic operations with a ktensor<a name="33"></a></h2><pre class="codeinput">+X <span class="comment">%<-- Calls uplus.</span></pre><pre class="codeoutput">ans is a ktensor of size 4 x 2 x 3 ans.lambda = [ 1 1 ] ans.U{1} = 0.4289 0.6822 0.3046 0.3028 0.1897 0.5417 0.1934 0.1509 ans.U{2} = 0.6979 0.8600 0.3784 0.8537 ans.U{3} = 0.5936 0.8216 0.4966 0.6449 0.8998 0.8180</pre><pre class="codeinput">-X <span class="comment">%<-- Calls uminus.</span></pre><pre class="codeoutput">ans is a ktensor of size 4 x 2 x 3 ans.lambda = [ -1 -1 ] ans.U{1} = 0.4289 0.6822 0.3046 0.3028 0.1897 0.5417 0.1934 0.1509 ans.U{2} = 0.6979 0.8600 0.3784 0.8537 ans.U{3} = 0.5936 0.8216 0.4966 0.6449 0.8998 0.8180</pre><pre class="codeinput">5*X <span class="comment">%<-- Calls mtimes.</span></pre><pre class="codeoutput">ans is a ktensor of size 4 x 2 x 3 ans.lambda = [ 5 5 ] ans.U{1} = 0.4289 0.6822 0.3046 0.3028 0.1897 0.5417 0.1934 0.1509 ans.U{2} = 0.6979 0.8600 0.3784 0.8537 ans.U{3} = 0.5936 0.8216 0.4966 0.6449 0.8998 0.8180</pre><h2>Use permute to reorder the modes of a ktensor<a name="36"></a></h2><pre class="codeinput">permute(X,[2 3 1]) <span class="comment">%<-- Reorders modes of X</span></pre><pre class="codeoutput">ans is a ktensor of size 2 x 3 x 4 ans.lambda = [ 1 1 ] ans.U{1} = 0.6979 0.8600 0.3784 0.8537 ans.U{2} = 0.5936 0.8216 0.4966 0.6449 0.8998 0.8180 ans.U{3} = 0.4289 0.6822 0.3046 0.3028 0.1897 0.5417 0.1934 0.1509</pre><h2>Use arrange to normalize the factors of a ktensor<a name="37"></a></h2> <p>The function <tt>arrange</tt> normalizes the columns of the factors and then arranges the rank-one pieces in decreasing order of size. </p><pre class="codeinput">X = ktensor({rand(3,2),rand(4,2),rand(2,2)}) <span class="comment">% <-- Unit weights.</span></pre><pre class="codeoutput">X is a ktensor of size 3 x 4 x 2 X.lambda = [ 1 1 ] X.U{1} = 0.8801 0.2714 0.1730 0.2523 0.9797 0.8757 X.U{2} = 0.7373 0.1991 0.1365 0.2987 0.0118 0.6614 0.8939 0.2844 X.U{3} = 0.4692 0.9883 0.0648 0.5828</pre><pre class="codeinput">arrange(X) <span class="comment">%<-- Normalized a rearranged.</span></pre><pre class="codeoutput">ans is a ktensor of size 3 x 4 x 2 ans.lambda = [ 0.87781 0.73416 ] ans.U{1} = 0.2855 0.6626 0.2653 0.1302
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?