在Matlab中,可以使用邏輯索引來刪除向量中的零分量。具體步驟如下:
vec = [1, 0, 3, 0, 5];
non_zero_idx = vec ~= 0; non_zero_elements = vec(non_zero_idx);