HOWTO · MATLAB

在 MATLAB 中檢查陣列或矩陣是否為空

本教程演示如何在 MATLAB 中確定陣列或矩陣是否為空。

本頁內容

一個簡單的空矩陣是維度為 0 x 0 的矩陣。維度為 0 x nn x 0 的矩陣也是空的。

本教程演示如何使用 isempty() 檢查陣列或矩陣是否為空。

在 MATLAB 中使用 isempty() 確定陣列或矩陣是否為空

isempty() 方法檢查矩陣或陣列是否為空。如果陣列為空,它將返回 1 為真,否則返回 0 為假。

例子:

A = [1, 2, 3];
B= [];
C = rand(2,2,2);
D = [1 3 5; 2 4 6; 7 8 10];
D(:,:,:) = []; %% Deleting the members of D matrix

disp('The Result for array A is: '); disp(isempty(A))
disp('The Result for array B is: '); disp(isempty(B))
disp('The Result for Matrix C is: '); disp(isempty(C))
disp('The Result for Matrix D is: '); disp(isempty(D))

輸出:

The Result for array A is: 
     0

The Result for array B is: 
     1

The Result for Matrix C is: 
     0

The Result for Matrix D is: 
     1