Main Content

matlab.unittest.constraints.IsSparse 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试数组是否稀疏

描述

matlab.unittest.constraints.IsSparse 类提供一个约束来测试数组是否为稀疏数组。

创建对象

描述

示例

c = matlab.unittest.constraints.IsSparse 创建一个约束来测试数组是否为稀疏数组。如果数组的存储类是稀疏的,则满足该约束。

示例

全部折叠

使用 IsSparse 约束测试数值数组。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSparse

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

测试单位矩阵是否稀疏。测试失败。

I = eye(7);
testCase.verifyThat(I,IsSparse)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSparse failed.
    --> The value must be sparse.
    
    Actual Value:
         1     0     0     0     0     0     0
         0     1     0     0     0     0     0
         0     0     1     0     0     0     0
         0     0     0     1     0     0     0
         0     0     0     0     1     0     0
         0     0     0     0     0     1     0
         0     0     0     0     0     0     1

I 转换为稀疏矩阵,然后再次测试。测试通过。

S = sparse(I);
testCase.verifyThat(S,IsSparse)
Verification passed.

版本历史记录

在 R2013a 中推出