📄 d_ktensor_doc.html
字号:
0.9209 0.7376 ans.U{2} = 0.2475 0.6319 0.3713 0.1170 0.8221 0.0101 0.3535 0.7661 ans.U{3} = 0.8614 0.9906 0.5079 0.1368</pre><h2>Use fixsigns for sign indeterminacies in a ktensor<a name="39"></a></h2> <p>The largest magnitude entry for each factor is changed to be positive provided that we can flip the signs of <i>pairs</i> of vectors in that rank-1 component. </p><pre class="codeinput">Y = X;Y.u{1}(:,1) = -Y.u{1}(:,1); <span class="comment">% switch the sign on a pair of columns</span>Y.u{2}(:,1) = -Y.u{2}(:,1)</pre><pre class="codeoutput">Y is a ktensor of size 3 x 4 x 2 Y.lambda = [ 1 1 ] Y.U{1} = -0.8801 0.2714 -0.1730 0.2523 -0.9797 0.8757 Y.U{2} = -0.7373 0.1991 -0.1365 0.2987 -0.0118 0.6614 -0.8939 0.2844 Y.U{3} = 0.4692 0.9883 0.0648 0.5828</pre><pre class="codeinput">fixsigns(Y)</pre><pre class="codeoutput">ans is a ktensor of size 3 x 4 x 2 ans.lambda = [ 1 1 ] ans.U{1} = 0.8801 0.2714 0.1730 0.2523 0.9797 0.8757 ans.U{2} = 0.7373 0.1991 0.1365 0.2987 0.0118 0.6614 0.8939 0.2844 ans.U{3} = 0.4692 0.9883 0.0648 0.5828</pre><h2>Use ktensor to store the 'skinny' SVD of a matrix<a name="41"></a></h2><pre class="codeinput">A = rand(4,3) <span class="comment">%<-- A random matrix.</span></pre><pre class="codeoutput">A = 0.4235 0.2259 0.6405 0.5155 0.5798 0.2091 0.3340 0.7604 0.3798 0.4329 0.5298 0.7833</pre><pre class="codeinput">[U,S,V] = svd(A,0); <span class="comment">%<-- Compute the SVD.</span>X = ktensor(diag(S),{U,V}) <span class="comment">%<-- Store the SVD as a ktensor.</span></pre><pre class="codeoutput">X is a ktensor of size 4 x 3 X.lambda = [ 1.7002 0.50951 0.22772 ] X.U{1} = -0.4346 -0.5816 0.3635 -0.4365 0.5184 0.6947 -0.5109 0.4983 -0.5366 -0.5996 -0.3804 -0.3120 X.U{2} = -0.4937 0.0444 0.8685 -0.6220 0.6800 -0.3883 -0.6078 -0.7319 -0.3080</pre><pre class="codeinput">double(X) <span class="comment">%<-- Reassemble the original matrix.</span></pre><pre class="codeoutput">ans = 0.4235 0.2259 0.6405 0.5155 0.5798 0.2091 0.3340 0.7604 0.3798 0.4329 0.5298 0.7833</pre><h2>Displaying a ktensor<a name="44"></a></h2><pre class="codeinput">disp(X) <span class="comment">%<-- Displays the vector lambda and each factor matrix.</span></pre><pre class="codeoutput">ans is a ktensor of size 4 x 3 ans.lambda = [ 1.7002 0.50951 0.22772 ] ans.U{1} = -0.4346 -0.5816 0.3635 -0.4365 0.5184 0.6947 -0.5109 0.4983 -0.5366 -0.5996 -0.3804 -0.3120 ans.U{2} = -0.4937 0.0444 0.8685 -0.6220 0.6800 -0.3883 -0.6078 -0.7319 -0.3080</pre><h2>Displaying data<a name="45"></a></h2> <p>The <tt>datadisp</tt> function allows the user to associate meaning to the modes and display those modes with the most meaning (i.e., corresponding to the largest values). </p><pre class="codeinput">X = ktensor({[0.8 0.1 1e-10]',[1e-5 2 3 1e-4]',[0.5 0.5]'}); <span class="comment">%<-- Create tensor.</span>X = arrange(X) <span class="comment">%<-- Normalize the factors.</span></pre><pre class="codeoutput">X is a ktensor of size 3 x 4 x 2 X.lambda = [ 2.0555 ] X.U{1} = 0.9923 0.1240 0.0000 X.U{2} = 0.0000 0.5547 0.8321 0.0000 X.U{3} = 0.7071 0.7071</pre><pre class="codeinput">labelsDim1 = {<span class="string">'one'</span>,<span class="string">'two'</span>,<span class="string">'three'</span>}; <span class="comment">%<-- Labels for mode 1.</span>labelsDim2 = {<span class="string">'A'</span>,<span class="string">'B'</span>,<span class="string">'C'</span>,<span class="string">'D'</span>}; <span class="comment">%<-- Labels for mode 2.</span>labelsDim3 = {<span class="string">'on'</span>,<span class="string">'off'</span>}; <span class="comment">%<-- Labels for mode 3.</span>datadisp(X,{labelsDim1,labelsDim2,labelsDim3}) <span class="comment">%<-- Display.</span></pre><pre class="codeoutput">======== Group 1 ========Weight = 2.055480Score Id Name 0.9922779 1 one 0.1240347 2 twoScore Id Name 0.8320503 3 C 0.5547002 2 B2.774e-005 4 D2.774e-006 1 AScore Id Name 0.7071068 1 on 0.7071068 2 off</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Kruskal tensors
% Kruskal format is a decomposition of a tensor X as the sum of the outer
% products a the columns of matrices. For example, we might write
%
% $${\mathcal X} = \sum_r a_r \circ b_r \circ c_r$$
%
% where a subscript denotes column index and a circle denotes outer
% product. In other words, the tensor X is built frm the columns of the
% matrices A,B, and C. It's often helpful to explicitly specify a weight
% for each outer product, which we do here:
%
% $${\mathcal X} = \sum_r \lambda_r \; a_r \circ b_r \circ c_r$$
%
% The |ktensor| class stores the components of the tensor X and can perform
% many operations, e.g., |ttm|, without explicitly forming the tensor X.
%% Kruskal tensor format via ktensor
% Kruskal format stores a tensor as a sum of rank-1 outer products. For
% example, consider a tensor of the following form.
%
% $$X = a_1 \circ b_1 \circ c_1 + a_2 \circ b_2 \circ c_2$$
%
% This can be stored in Kruskal form as follows.
rand('state',0);
A = rand(4,2); %<REPLACE_WITH_DASH_DASH First column is a_1, second is a_2.
B = rand(3,2); %<REPLACE_WITH_DASH_DASH Likewise for B.
C = rand(2,2); %<REPLACE_WITH_DASH_DASH Likewise for C.
X = ktensor({A,B,C}) %<REPLACE_WITH_DASH_DASH Create the ktensor.
%%
% For Kruskal format, there can be any number of matrices, but every matrix
% must have the same number of columns. The number of rows can vary.
Y = ktensor({rand(4,1),rand(2,1),rand(3,1)}) %<REPLACE_WITH_DASH_DASH Another ktensor.
%% Specifying weights in a ktensor
% Weights for each rank-1 tensor can be specified by passing in a
% column vector. For example,
%
% $$X = \lambda_1 \; a_1 \circ b_1 \circ c_1 + \lambda_2 \; a_2 \circ b_2 \circ c_2$$
%
lambda = [5.0; 0.25]; %<REPLACE_WITH_DASH_DASH Weights for each factor.
X = ktensor(lambda,{A,B,C}) %<REPLACE_WITH_DASH_DASH Create the ktensor.
%% Creating a one-dimensional ktensor
Y = ktensor({rand(4,5)}) %<REPLACE_WITH_DASH_DASH A one-dimensional ktensor.
%% Constituent parts of a ktensor
X.lambda %<REPLACE_WITH_DASH_DASH Weights or multipliers.
%%
X.U %<REPLACE_WITH_DASH_DASH Cell array of matrices.
%% Creating a ktensor from its constituent parts
Y = ktensor(X.lambda,X.U) %<REPLACE_WITH_DASH_DASH Recreate X.
%% Creating an empty ktensor
Z = ktensor %<REPLACE_WITH_DASH_DASH Empty ktensor.
%% Use full or tensor to convert a ktensor to a tensor
full(X) %<REPLACE_WITH_DASH_DASH Converts to a tensor.
%%
tensor(X) %<REPLACE_WITH_DASH_DASH Same as above.
%% Use double to convert a ktensor to a multidimensional array
double(X) %<REPLACE_WITH_DASH_DASH Converts to an array.
%% Use tendiag or sptendiag to convert a ktensor to a ttensor.
% A ktensor can be regarded as a ttensor with a diagonal core.
R = length(X.lambda); %<REPLACE_WITH_DASH_DASH Number of factors in X.
core = tendiag(X.lambda, repmat(R,1,ndims(X))); %<REPLACE_WITH_DASH_DASH Create a diagonal core.
Y = ttensor(core, X.u) %<REPLACE_WITH_DASH_DASH Assemble the ttensor.
%%
norm(full(X)-full(Y)) %<REPLACE_WITH_DASH_DASH They are the same.
%%
core = sptendiag(X.lambda, repmat(R,1,ndims(X))); %<REPLACE_WITH_DASH_DASH Sparse diagonal core.
Y = ttensor(core, X.u) %<REPLACE_WITH_DASH_DASH Assemble the ttensor
%%
norm(full(X)-full(Y)) %<REPLACE_WITH_DASH_DASH They are the same.
%% Use ndims and size for the dimensions of a ktensor
ndims(X) %<REPLACE_WITH_DASH_DASH Number of dimensions.
%%
size(X) %<REPLACE_WITH_DASH_DASH Row vector of the sizes.
%%
size(X,2) %<REPLACE_WITH_DASH_DASH Size of the 2nd mode.
%% Subscripted reference for a ktensor
X(1,1,1) %<REPLACE_WITH_DASH_DASH Assemble the (1,1,1) element (requires computation).
%%
X.lambda(2) %<REPLACE_WITH_DASH_DASH Weight of 2nd factor.
%%
X.U{2} %<REPLACE_WITH_DASH_DASH Extract a matrix.
%%
X{2} %<REPLACE_WITH_DASH_DASH Same as above.
%% Subscripted assignment for a ktensor
X.lambda = ones(size(X.lambda)) %<REPLACE_WITH_DASH_DASH Insert new multipliers.
%%
X.lambda(1) = 7 %<REPLACE_WITH_DASH_DASH Change a single element of lambda.
%%
X{3}(1:2,1) = [1;1] %<REPLACE_WITH_DASH_DASH Change the matrix for mode 3.
%% Use end for the last array index.
X(3:end,1,1) %<REPLACE_WITH_DASH_DASH Calculated X(3,1,1) and X((4,1,1).
%%
X(1,1,1:end-1) %<REPLACE_WITH_DASH_DASH Calculates X(1,1,1).
%%
X{end} %<REPLACE_WITH_DASH_DASH Or use inside of curly braces. This is X{3}.
%% Adding and subtracting ktensors
% Adding two ktensors is the same as concatenating the matrices
X = ktensor({rand(4,2),rand(2,2),rand(3,2)}) %<REPLACE_WITH_DASH_DASH Data.
Y = ktensor({rand(4,2),rand(2,2),rand(3,2)}) %<REPLACE_WITH_DASH_DASH More data.
%%
Z = X + Y %<REPLACE_WITH_DASH_DASH Concatenates the factor matrices.
%%
Z = X - Y %<REPLACE_WITH_DASH_DASH Concatenates as with plus, but changes the weights.
%%
norm( full(Z) - (full(X)-full(Y)) ) %<REPLACE_WITH_DASH_DASH Should be zero.
%% Basic operations with a ktensor
+X %<REPLACE_WITH_DASH_DASH Calls uplus.
%%
-X %<REPLACE_WITH_DASH_DASH Calls uminus.
%%
5*X %<REPLACE_WITH_DASH_DASH Calls mtimes.
%% Use permute to reorder the modes of a ktensor
permute(X,[2 3 1]) %<REPLACE_WITH_DASH_DASH Reorders modes of X
%% Use arrange to normalize the factors of a ktensor
% The function |arrange| normalizes the columns of the factors and then
% arranges the rank-one pieces in decreasing order of size.
X = ktensor({rand(3,2),rand(4,2),rand(2,2)}) % <REPLACE_WITH_DASH_DASH Unit weights.
%%
arrange(X) %<REPLACE_WITH_DASH_DASH Normalized a rearranged.
%% Use fixsigns for sign indeterminacies in a ktensor
% The largest magnitude entry for each factor is changed to be
% positive provided that we can flip the signs of _pairs_ of vectors in
% that rank-1 component.
Y = X;
Y.u{1}(:,1) = -Y.u{1}(:,1); % switch the sign on a pair of columns
Y.u{2}(:,1) = -Y.u{2}(:,1)
%%
fixsigns(Y)
%% Use ktensor to store the 'skinny' SVD of a matrix
A = rand(4,3) %<REPLACE_WITH_DASH_DASH A random matrix.
%%
[U,S,V] = svd(A,0); %<REPLACE_WITH_DASH_DASH Compute the SVD.
X = ktensor(diag(S),{U,V}) %<REPLACE_WITH_DASH_DASH Store the SVD as a ktensor.
%%
double(X) %<REPLACE_WITH_DASH_DASH Reassemble the original matrix.
%% Displaying a ktensor
disp(X) %<REPLACE_WITH_DASH_DASH Displays the vector lambda and each factor matrix.
%% Displaying data
% The |datadisp| function allows the user to associate meaning to the modes
% and display those modes with the most meaning (i.e., corresponding to the
% largest values).
X = ktensor({[0.8 0.1 1e-10]',[1e-5 2 3 1e-4]',[0.5 0.5]'}); %<REPLACE_WITH_DASH_DASH Create tensor.
X = arrange(X) %<REPLACE_WITH_DASH_DASH Normalize the factors.
%%
labelsDim1 = {'one','two','three'}; %<REPLACE_WITH_DASH_DASH Labels for mode 1.
labelsDim2 = {'A','B','C','D'}; %<REPLACE_WITH_DASH_DASH Labels for mode 2.
labelsDim3 = {'on','off'}; %<REPLACE_WITH_DASH_DASH Labels for mode 3.
datadisp(X,{labelsDim1,labelsDim2,labelsDim3}) %<REPLACE_WITH_DASH_DASH Display.##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -