主要内容

mustBeVector

R2026b

验证值为向量

    描述

    如果 value 不是向量,则 mustBeVector(value) 会抛出错误。向量的维度为 1×n 或 n×1。此函数不返回值。mustBeVector 调用 isvector 函数来确定输入是否为向量。

    支持的类:实现 isvector 的所有数值类、logical、char、string 和 MATLAB® 类。

    示例

    示例

    全部折叠

    使用 mustBeVector 验证 a 是行向量或列向量。

    a = rand(2);
    mustBeVector(a)
    Value must be a 1-by-n vector or an n-by-1 vector.

    mustBeVector 会抛出错误,因为输入是 2×2 数组。

    将值 a 重构为行向量。

    b = reshape(a,[1,numel(a)])
    mustBeVector(b)

    mustBeVector 执行时不会抛出错误或返回值。

    使用 arguments 代码块通过 mustBeVector 和 mustBeNumeric 将函数输入限制为数值向量。

    WeeklyTotals 函数对输入向量的元素求和。

    function r = WeeklyTotals(DailyTotals)
        arguments
            DailyTotals {mustBeVector,mustBeNumeric}
        end
        r = sum(DailyTotals);
    end

    传递行向量或列向量会返回向量元素之和。

    r = WeeklyTotals(1:5)
    r = 
        15

    输入参数

    全部折叠

    要验证的值,指定为行向量或列向量。如果指定的值不是向量,该函数会抛出错误。

    数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | categorical | datetime | duration | calendarDuration
    复数支持: 是

    提示

    • mustBeVector 用于属性和函数参量验证。

    扩展功能

    全部展开

    C/C++ 代码生成
    使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

    GPU 代码生成
    使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。

    版本历史记录

    在 R2020b 中推出

    全部展开