📄 q_collapse_scale_doc.html
字号:
<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>Collapsing and scaling tensors</title> <meta name="generator" content="MATLAB 7.2"> <meta name="date" content="2007-01-10"> <meta name="m-file" content="Q_collapse_scale_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>Collapsing and scaling tensors</h1> <introduction> <p>The tensor and sptensor classes support that notion of collapsing and scaling dimensions.</p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Examples of collapsing a tensor</a></li> <li><a href="#6">Alternate accumulation functions for tensor</a></li> <li><a href="#8">Examples of collapsing a sptensor</a></li> <li><a href="#13">Alternate accumulation functions for sptensor</a></li> <li><a href="#15">Scaling a tensor in different modes</a></li> <li><a href="#21">Scaling a sptensor in different modes</a></li> </ul> </div> <h2>Examples of collapsing a tensor<a name="1"></a></h2><pre class="codeinput">X = tenrand([4 3 2]) <span class="comment">%<-- Generate some data.</span></pre><pre class="codeoutput">X is a tensor of size 4 x 3 x 2 X(:,:,1) = 0.6408 0.1708 0.3142 0.1909 0.9943 0.3651 0.8439 0.4398 0.3932 0.1739 0.3400 0.5915 X(:,:,2) = 0.1197 0.9342 0.2379 0.0381 0.2644 0.6458 0.4586 0.1603 0.9669 0.8699 0.8729 0.6649</pre><pre class="codeinput">Y = collapse(X,[2 3]) <span class="comment">%<-- Sum of entries in each mode-1 slice.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 2.4177 2.4987 3.2627 3.5131</pre><pre class="codeinput">Y = collapse(X,-1) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 4 Y(:) = 2.4177 2.4987 3.2627 3.5131</pre><pre class="codeinput">Z = collapse(X,2) <span class="comment">%<-- Sum of entries in each row fiber.</span></pre><pre class="codeoutput">Z is a tensor of size 4 x 2 Z(:,:) = 1.1258 1.2919 1.5503 0.9484 1.6769 1.5858 1.1055 2.4077</pre><pre class="codeinput">collapse(X,1:3) <span class="comment">%<-- Sum of all entries.</span></pre><pre class="codeoutput">ans = 11.6922</pre><h2>Alternate accumulation functions for tensor<a name="6"></a></h2><pre class="codeinput">Y = collapse(X,[1 2],@max) <span class="comment">%<-- Max entry in each mode-3 slice.</span></pre><pre class="codeoutput">Y is a tensor of size 2 Y(:) = 0.9943 0.9669</pre><pre class="codeinput">Z = collapse(X,-3,@mean) <span class="comment">%<-- Average entry in each mode-3 slice.</span></pre><pre class="codeoutput">Z is a tensor of size 2 Z(:) = 0.4549 0.5195</pre><h2>Examples of collapsing a sptensor<a name="8"></a></h2><pre class="codeinput">X = sptenrand([4 3 2],6) <span class="comment">%<-- Generate some data.</span></pre><pre class="codeoutput">X is a sparse tensor of size 4 x 3 x 2 with 6 nonzeros (1,2,1) 0.4507 (1,3,2) 0.4122 (2,1,1) 0.9016 (4,1,1) 0.0056 (4,1,2) 0.2974 (4,3,1) 0.0492</pre><pre class="codeinput">Y = collapse(X,[2 3]) <span class="comment">%<-- Sum of entries in each mode-1 slice.</span></pre><pre class="codeoutput">Y = 0.8629 0.9016 0 0.3522</pre><pre class="codeinput">Y = collapse(X,-1) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y = 0.8629 0.9016 0 0.3522</pre><pre class="codeinput">Z = collapse(X,2) <span class="comment">%<-- Sum of entries in each row fiber.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 4 x 2 with 5 nonzeros (1,1) 0.4507 (1,2) 0.4122 (2,1) 0.9016 (4,1) 0.0547 (4,2) 0.2974</pre><pre class="codeinput">collapse(X,1:3) <span class="comment">%<-- Sum of all entries.</span></pre><pre class="codeoutput">ans = 2.1167</pre><h2>Alternate accumulation functions for sptensor<a name="13"></a></h2><pre class="codeinput">Y = collapse(X,[1 2],@min) <span class="comment">%<-- Min *nonzero* entry in each mode-3 slice.</span></pre><pre class="codeoutput">Y = 0.0056 0.2974</pre><pre class="codeinput">Z = collapse(X,-3,@mean) <span class="comment">%<-- Average *nonzero* entry in each mode-3 slice.</span></pre><pre class="codeoutput">Z = 0.3518 0.3548</pre><h2>Scaling a tensor in different modes<a name="15"></a></h2><pre class="codeinput">X = tenones([3,4,5]); <span class="comment">%<-- Generate data</span>S = 10 * [1:5]'; Y = scale(X,S,3) <span class="comment">%<-- Scale in mode-3</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 10 10 10 10 10 10 10 10 10 10 10 10 Y(:,:,2) = 20 20 20 20 20 20 20 20 20 20 20 20 Y(:,:,3) = 30 30 30 30 30 30 30 30 30 30 30 30 Y(:,:,4) = 40 40 40 40 40 40 40 40 40 40 40 40 Y(:,:,5) = 50 50 50 50 50 50 50 50 50 50 50 50</pre><pre class="codeinput">S = tensor(10 * [1:5]',5); Y = scale(X,S,3) <span class="comment">%<-- First argument is a tensor.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 10 10 10 10 10 10 10 10 10 10 10 10 Y(:,:,2) = 20 20 20 20 20 20 20 20 20 20 20 20 Y(:,:,3) = 30 30 30 30 30 30 30 30 30 30 30 30 Y(:,:,4) = 40 40 40 40 40 40 40 40 40 40 40 40 Y(:,:,5) = 50 50 50 50 50 50 50 50 50 50 50 50</pre><pre class="codeinput">S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) <span class="comment">%<-- Scale in two modes.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,2) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,3) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,4) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,5) = 1 4 7 10 2 5 8 11 3 6 9 12</pre><pre class="codeinput">S = tensor(1:12,[3 4]); Y = scale(X,S,-3) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,2) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,3) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,4) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,5) = 1 4 7 10 2 5 8 11 3 6 9 12</pre><pre class="codeinput">S = tensor(1:60,[3 4 5]); Y = scale(X,S,1:3) <span class="comment">%<-- Scale in every mode.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,2) = 13 16 19 22 14 17 20 23 15 18 21 24 Y(:,:,3) = 25 28 31 34 26 29 32 35 27 30 33 36 Y(:,:,4) = 37 40 43 46 38 41 44 47 39 42 45 48 Y(:,:,5) = 49 52 55 58 50 53 56 59 51 54 57 60</pre><pre class="codeinput">Y = S .* X <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a tensor of size 3 x 4 x 5 Y(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 Y(:,:,2) = 13 16 19 22 14 17 20 23 15 18 21 24 Y(:,:,3) = 25 28 31 34 26 29 32 35 27 30 33 36 Y(:,:,4) = 37 40 43 46 38 41 44 47 39 42 45 48 Y(:,:,5) = 49 52 55 58 50 53 56 59 51 54 57 60</pre><h2>Scaling a sptensor in different modes<a name="21"></a></h2><pre class="codeinput">X = ones(sptenrand([3 4 5], 10)) <span class="comment">%<-- Generate data.</span></pre><pre class="codeoutput">X is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 1 (1,4,4) 1 (2,1,4) 1 (2,3,4) 1 (2,4,3) 1 (2,4,4) 1 (2,4,5) 1 (3,2,3) 1 (3,3,2) 1 (3,4,2) 1</pre><pre class="codeinput">S = 10 * [1:5]'; Y = scale(X,S,3) <span class="comment">%<-- Scale in one mode.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 20 (1,4,4) 40 (2,1,4) 40 (2,3,4) 40 (2,4,3) 30 (2,4,4) 40 (2,4,5) 50 (3,2,3) 30 (3,3,2) 20 (3,4,2) 20</pre><pre class="codeinput">S = tensor(10 * [1:5]',5); Y = scale(X,S,3) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 20 (1,4,4) 40 (2,1,4) 40 (2,3,4) 40 (2,4,3) 30 (2,4,4) 40 (2,4,5) 50 (3,2,3) 30 (3,3,2) 20 (3,4,2) 20</pre><pre class="codeinput">S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) <span class="comment">%<-- Scale in two modes.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 4 (1,4,4) 10 (2,1,4) 2 (2,3,4) 8 (2,4,3) 11 (2,4,4) 11 (2,4,5) 11 (3,2,3) 6 (3,3,2) 9 (3,4,2) 12</pre><pre class="codeinput">S = tensor(1:12,[3 4]); Y = scale(X,S,-3) <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 4 (1,4,4) 10 (2,1,4) 2 (2,3,4) 8 (2,4,3) 11 (2,4,4) 11 (2,4,5) 11 (3,2,3) 6 (3,3,2) 9 (3,4,2) 12</pre><pre class="codeinput">Z = scale(X,Y,1:3) <span class="comment">%<-- Scale by a sparse tensor.</span></pre><pre class="codeoutput">Z is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 4 (1,4,4) 10 (2,1,4) 2 (2,3,4) 8 (2,4,3) 11 (2,4,4) 11 (2,4,5) 11 (3,2,3) 6 (3,3,2) 9 (3,4,2) 12</pre><pre class="codeinput">X .* Y <span class="comment">%<-- Same as above.</span></pre><pre class="codeoutput">ans is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros (1,2,2) 4 (1,4,4) 10 (2,1,4) 2 (2,3,4) 8 (2,4,3) 11 (2,4,4) 11 (2,4,5) 11 (3,2,3) 6 (3,3,2) 9 (3,4,2) 12</pre><p class="footer"><br> Published with MATLAB® 7.2<br></p> </div> <!--##### SOURCE BEGIN #####%% Collapsing and scaling tensors
% The tensor and sptensor classes support that notion of collapsing and
% scaling dimensions.
%% Examples of collapsing a tensor
X = tenrand([4 3 2]) %<REPLACE_WITH_DASH_DASH Generate some data.
%%
Y = collapse(X,[2 3]) %<REPLACE_WITH_DASH_DASH Sum of entries in each mode-1 slice.
%%
Y = collapse(X,-1) %<REPLACE_WITH_DASH_DASH Same as above.
%%
Z = collapse(X,2) %<REPLACE_WITH_DASH_DASH Sum of entries in each row fiber.
%%
collapse(X,1:3) %<REPLACE_WITH_DASH_DASH Sum of all entries.
%% Alternate accumulation functions for tensor
Y = collapse(X,[1 2],@max) %<REPLACE_WITH_DASH_DASH Max entry in each mode-3 slice.
%%
Z = collapse(X,-3,@mean) %<REPLACE_WITH_DASH_DASH Average entry in each mode-3 slice.
%% Examples of collapsing a sptensor
X = sptenrand([4 3 2],6) %<REPLACE_WITH_DASH_DASH Generate some data.
%%
Y = collapse(X,[2 3]) %<REPLACE_WITH_DASH_DASH Sum of entries in each mode-1 slice.
%%
Y = collapse(X,-1) %<REPLACE_WITH_DASH_DASH Same as above.
%%
Z = collapse(X,2) %<REPLACE_WITH_DASH_DASH Sum of entries in each row fiber.
%%
collapse(X,1:3) %<REPLACE_WITH_DASH_DASH Sum of all entries.
%% Alternate accumulation functions for sptensor
Y = collapse(X,[1 2],@min) %<REPLACE_WITH_DASH_DASH Min *nonzero* entry in each mode-3 slice.
%%
Z = collapse(X,-3,@mean) %<REPLACE_WITH_DASH_DASH Average *nonzero* entry in each mode-3 slice.
%% Scaling a tensor in different modes
X = tenones([3,4,5]); %<REPLACE_WITH_DASH_DASH Generate data
S = 10 * [1:5]'; Y = scale(X,S,3) %<REPLACE_WITH_DASH_DASH Scale in mode-3
%%
S = tensor(10 * [1:5]',5); Y = scale(X,S,3) %<REPLACE_WITH_DASH_DASH First argument is a tensor.
%%
S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) %<REPLACE_WITH_DASH_DASH Scale in two modes.
%%
S = tensor(1:12,[3 4]); Y = scale(X,S,-3) %<REPLACE_WITH_DASH_DASH Same as above.
%%
S = tensor(1:60,[3 4 5]); Y = scale(X,S,1:3) %<REPLACE_WITH_DASH_DASH Scale in every mode.
%%
Y = S .* X %<REPLACE_WITH_DASH_DASH Same as above.
%% Scaling a sptensor in different modes
X = ones(sptenrand([3 4 5], 10)) %<REPLACE_WITH_DASH_DASH Generate data.
%%
S = 10 * [1:5]'; Y = scale(X,S,3) %<REPLACE_WITH_DASH_DASH Scale in one mode.
%%
S = tensor(10 * [1:5]',5); Y = scale(X,S,3) %<REPLACE_WITH_DASH_DASH Same as above.
%%
S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) %<REPLACE_WITH_DASH_DASH Scale in two modes.
%%
S = tensor(1:12,[3 4]); Y = scale(X,S,-3) %<REPLACE_WITH_DASH_DASH Same as above.
%%
Z = scale(X,Y,1:3) %<REPLACE_WITH_DASH_DASH Scale by a sparse tensor.
%%
X .* Y %<REPLACE_WITH_DASH_DASH Same as above.##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -