disp.m

来自「张量分析工具」· M 代码 · 共 86 行

M
86
字号
function disp(X,name)%DISP Command window display of a sparse tensor.%%   DISP(X) displays the tensor without printing its name.%%   DISP(X,NAME) displays the tensor with the given name.%%   See also SPTENSOR, SPTENSOR/DISPLAY.%%MATLAB Tensor Toolbox.%Copyright 2007, Sandia Corporation. % This is the MATLAB Tensor Toolbox by Brett Bader and Tamara Kolda. % http://csmr.ca.sandia.gov/~tgkolda/TensorToolbox.% Copyright (2007) Sandia Corporation. Under the terms of Contract% DE-AC04-94AL85000, there is a non-exclusive license for use of this% work by or on behalf of the U.S. Government. Export of this data may% require a license from the United States Government.% The full license terms can be found in tensor_toolbox/LICENSE.txt% $Id: disp.m,v 1.18 2007/01/10 01:27:31 bwbader Exp $% Extract the number of nonzeros and number of dimensionsnz = nnz(X);if ~exist('name','var')    name = 'ans';endif (nz == 0)    fprintf('%s is an all-zero sparse tensor of size %s\n',...        name, tt_size2str(X.size));    return;else    fprintf('%s is a sparse tensor of size %s with %d nonzeros\n',...        name, tt_size2str(X.size), nz);end% Stop insane printoutsif (nz > 10000)    r = input('Are you sure you want to print all nonzeros? (Y/N) ','s');    if upper(r) ~= 'Y', return, end;end% preallocateoutput = cell(nz,1);%%spc = floor(log10(max(X.subs,[],1)))+1;if numel(spc) == 1    fmt = ['\t(%' num2str(spc(1)) 'd)%s'];else    fmt = ['\t(%' num2str(spc(1)) 'd,'];    for i = 2:numel(spc)-1        fmt = [fmt '%' num2str(spc(i)) 'd,'];    end    fmt = [fmt '%' num2str(spc(end)) 'd)%s'];end%%% Get values out so that they look nicesavefmt = get(0,'FormatSpacing');format compactS = evalc('disp(X.vals)');set(0,'FormatSpacing',savefmt)S = textscan(S,'%s','delimiter','\n','whitespace','');S = S{1};if ~isempty(strfind(S{1},'*'))    fprintf('%s\n',S{1});    S = S(2:end);end%%for i = 1:nz    output{i} = sprintf(fmt,X.subs(i,:),S{i});endfprintf('%s\n',output{:});%     function y = fmt(s,v)%         % nested function has access to n from disp function workspace%         if n > 1%             y = [sprintf('\t(') sprintf('%d,',s(1:n-1))...%                 sprintf('%d) ',s(n)) sprintf('\t%g',v)];%         else%             y = sprintf('\t(%d) \t%f',s,v);%         end%     endend

⌨️ 快捷键说明

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