matlab中,怎样把矩阵中的0元素删除

2025-01-01 21:07:37
推荐回答(1个)
回答1:

(1)如果是一维的,a(a==0) = [] 就能解决问题如果是多维的,a(a==0) = [] 会把a变成一维的。a =4 4 4 2 24 0 3 2 22 1 2 4 33 0 5 4 41 0 0 1 4>> a(a==0)=[]a =Columns 1 through 204 4 2 3 1 4 1 4 3 2 5 2 2 4 4 1 2 2 3 4Column 214(2) 如果是多维的,a(a==0) =NaN 就不会把a变成一维的。a = 4 4 4 2 2 4 0 3 2 2 2 1 2 4 3 3 0 5 4 4 1 0 0 1 4>> a(a==0) =NaNa = 4 4 4 2 2 4 NaN 3 2 2 2 1 2 4 3 3 NaN 5 4 4 1 NaN NaN 1 4