【问题标题】:MATLAB: printmat with complex numbersMATLAB:带有复数的 printmat
【发布时间】:2013-11-17 18:09:11
【问题描述】:

我想使用 MATLAB 的 printmat 来显示带有标签的矩阵。但这不适用于复数:

N = 5; 

x = rand(N,1);
y = rand(N,1);
z = x + 1i*y;

printmat([x,y,z],'fftdemo','N=1 2 3 4 5','x y x+iy');

输出:

fftdemo = 
                     x            y         x+iy
      N=1      0.84072      0.34998      0.84072
        2      0.25428      0.19660      0.25428
        3      0.81428      0.25108      0.81428
        4      0.24352      0.61604      0.24352
        5      0.92926      0.47329      0.92926

如您所见,z 的虚部没有打印出来。

有没有办法让 Matlab 显示复数或其他方法来实现这一点?

【问题讨论】:

  • PRINTMAT 不是 The MathWorks 提供的函数(至少,不在核心 MATLAB 中)。您或您的一位同事一定是从某个地方得到的。尝试运行 help printmat 以查看创建者是否记录了它。
  • "Matlab 在控制系统工具箱中有一个叫做 printmat 的函数。它在目录 "ctrlobsolete" 中,所以我们可以假设它被认为是"过时的",但它仍然有效。" stackoverflow.com/a/13330770/2750945我还没有找到用于此目的的新功能。
  • 在命令窗口中输入edit printmat 以查看代码。复制它并创建一个新版本。编辑此新版本以打印复杂值。利润!

标签: matlab


【解决方案1】:

matlab 中的任何打印函数都只会打印虚数的实数值。要获得这两个部分,您必须显式调用它们。所以正确的用法是

 printmat([x,y,real(z),imag(z)],'fftdemo','N=1 2 3 4 5','x y x iy');

但这现在没有任何用处了,因为这两个部分都被打印了两次。

【讨论】:

    【解决方案2】:

    这里是printmat 的一个稍微改动的版本,它将打印复数。随意摆弄它以获得更好的外观:)

    function [] = printmat2(a,name,rlab,clab)
    %PRINTMAT Print matrix with labels.
    %   PRINTMAT(A,NAME,RLAB,CLAB) prints the matrix A with the row labels
    %   RLAB and column labels CLAB.  NAME is a string used to name the 
    %   matrix.  RLAB and CLAB are string variables that contain the row
    %   and column labels delimited by spaces.  For example, the string
    %
    %       RLAB = 'alpha beta gamma';
    %
    %   defines 'alpha' as the label for the first row, 'beta' for the
    %   second row and 'gamma' for the third row.  RLAB and CLAB must
    %   contain the same number of space delimited labels as there are 
    %   rows and columns respectively.
    %
    %   PRINTMAT(A,NAME) prints the matrix A with numerical row and column
    %   labels.  PRINTMAT(A) prints the matrix A without a name.
    %
    %   See also: PRINTSYS.
    
    %   Clay M. Thompson  9-24-90
    %   Copyright 1986-2002 The MathWorks, Inc. 
    %   $Revision: 1.10 $  $Date: 2002/04/10 06:32:35 $
    
    error(nargchk(1,4,nargin));
    
    space = ' ';
    [nrows,ncols] = size(a);
    
    if nargin<2, name = []; end
    if nargin==3, error('Wrong number of input arguments.'); end
    if nargin<4,
      rlab = []; clab = [];
      for i=1:nrows, rlab = [rlab, sprintf('--%d--> ',i)]; end
      for i=1:ncols, clab = [clab, sprintf('--%d--> ',i)]; end
      rlab=rlab(1:length(rlab)-1);
      clab=clab(1:length(clab)-1);
    end
    
    col_per_scrn=5;
    len=12;
    
    
    if (nrows==0)|(ncols==0), 
      if ~isempty(name), disp(' '), disp(sprintf('%s = ',name)), end
      disp(' ')
      disp('     []')
      disp(' ')
      return
    end
    
    % Remove extra spaces (delimiters)
    ndx1 = find(clab==' ');
    ndx2 = find([ndx1,0]==[-1,ndx1+1]);
    if ~isempty(clab), clab(ndx1(ndx2))=[]; end
    
    ndx1 = find(rlab==' ');
    ndx2 = find([ndx1,0]==[-1,ndx1+1]);
    if ~isempty(rlab), rlab(ndx1(ndx2))=[]; end
    
    % Determine position of delimiters
    cpos = find(clab==' ');
    if length(cpos)<ncols-1, error('Not enough column labels.'); end
    cpos = [0,cpos,length(clab)+1];
    
    rpos = find(rlab==' ');
    if length(rpos)<nrows-1, error('Not enough row labels.'); end
    rpos = [0,rpos,length(rlab)+1];
    
    col=1;
    n = min(col_per_scrn-1,ncols-1);
    disp(' ')
    if ~isempty(name), disp(sprintf('%s = ',name)), end  % Print name
    while col<=ncols
      % Print labels
      s = space(ones(1,len+1));
      for j=0:n,
        lab = clab(cpos(col+j)+1:cpos(col+j+1)-1);
        if length(lab)>len,
          lab=lab(1:len);
        else
          lab=[space(ones(1,len-length(lab))),lab]; end
        s= [s,' ',lab];
      end
      disp(setstr(s))
      for i=1:nrows,
          s = rlab(rpos(i)+1:rpos(i+1)-1);
          if length(s)>len, s=s(1:len); else s=[space(ones(1,len-length(s))),s]; end
          s = [' ',s];
          for j=0:n,
              element = a(i,col+j);
              if imag(element) ~= 0
                  s=[s,sprintf('%12.5f + %12.5fi',[real(element) imag(element)])];
              else
                  if element==0,
                      s=[s,'            0'];
                  elseif (element>=1.e6)|(element<=-1.e5)|(abs(element)<.0001)
                      s=[s,sprintf(' %12.5e',element)];
                  else
                      s=[s,sprintf(' %12.5f',element)];
                  end
              end
          end
        disp(s)
      end % for
      col = col+col_per_scrn;
      disp(' ')
      if (ncols-col<n), n=ncols-col; end
    end % while
    
    % end printmat
    

    【讨论】:

    • 我建议进行编辑,以解决一些问题,以防复数是否为负数或给定列中的所有数字都不是复数。但是标签仍然关闭,到目前为止我找不到解决此问题的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多