{
  "name": "MATLAB diag: input form, call and output size, worked cases",
  "description": "Worked output-size cases for the MATLAB diag function (build from a vector, extract from a matrix, offset diagonals, edge cases), compiled from the MathWorks diag reference page and its rules for size and range of k.",
  "page": "https://casrai.org/guides/diag-matrix-matlab",
  "publisher": "CASRAI",
  "license": "https://creativecommons.org/licenses/by/4.0/",
  "dateModified": "2026-11-16T22:02:25Z",
  "temporalCoverage": "2026",
  "spatialCoverage": null,
  "columns": [
    {
      "name": "Case",
      "unit": "",
      "description": ""
    },
    {
      "name": "Call",
      "unit": "",
      "description": ""
    },
    {
      "name": "Input size",
      "unit": "",
      "description": ""
    },
    {
      "name": "Output size",
      "unit": "",
      "description": ""
    },
    {
      "name": "Rule",
      "unit": "",
      "description": ""
    }
  ],
  "rows": [
    {
      "Case": "Vector to main diagonal",
      "Call": "diag(v)",
      "Input size": "1-by-5 vector",
      "Output size": "5-by-5",
      "Rule": "Square, order N"
    },
    {
      "Case": "Vector to first superdiagonal",
      "Call": "diag(v,1)",
      "Input size": "1-by-5 vector",
      "Output size": "6-by-6",
      "Rule": "Order N + abs(k)"
    },
    {
      "Case": "Vector to first subdiagonal",
      "Call": "diag(v,-1)",
      "Input size": "1-by-5 vector",
      "Output size": "6-by-6",
      "Rule": "Order N + abs(k)"
    },
    {
      "Case": "Main diagonal of a matrix",
      "Call": "diag(A)",
      "Input size": "6-by-6 matrix",
      "Output size": "6-by-1 column vector",
      "Rule": "Matrix in, column vector out"
    },
    {
      "Case": "First subdiagonal of a matrix",
      "Call": "diag(A,-1)",
      "Input size": "6-by-6 matrix",
      "Output size": "5-by-1 column vector",
      "Rule": "One fewer element than main diagonal"
    },
    {
      "Case": "Empty input",
      "Call": "diag([])",
      "Input size": "0-by-0",
      "Output size": "[] (empty)",
      "Rule": "Returns empty"
    }
  ],
  "sources": [
    {
      "title": "MathWorks diag documentation (R2026b)",
      "url": "https://www.mathworks.com/help/matlab/ref/diag.html",
      "accessed": "2026-11-12"
    }
  ]
}