trilow.m

来自「计量工具箱」· M 代码 · 共 43 行

M
43
字号
% TRILOW: Extracts the lower triangular portion (without diagonal) of a
%         square symmetric matrix into a columnwise column vector.
%         Optionally returns corresponding subscripts.
%         If given a scalar, returns it.
%
%         Use trisqmat() to reverse the extraction.
%
%     Usage: [c,i,j] = trilow(x)
%

% RE Strauss, 1/13/96
%   8/20/99 - subscripting changes due to new Matlab v5 conventions.

function [c,i,j] = trilow(x)
  [n,p] = size(x);
  if (n~=p)
    error('  Matrix must be square');
  end;

  if (n==1)
    c = x;
  else
    t = tril(ones(n)) - eye(n);
    t = t(:);
    x = x(:);
    c = x(t==1);
  end;

  if (nargout>1)
    i = ones(length(c),1);
    j = i;
    k = 0;
    for jj = 1:(n-1)
      for ii = (jj+1):n
        k = k+1;
        i(k) = ii;
        j(k) = jj;
      end;
    end;
  end;

  return;

⌨️ 快捷键说明

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