假设输入为:
[1 2 3;
2 3 3;
3 4 3;
3 5 3;]
预期输出为:
[1 2;
2 3;
3 4;
3 5;]
删除第三列的原因是因为第三列中的所有元素都相同。是否有默认的matlab函数?
答案 0 :(得分:1)
A(:,sum(abs(diff(A)))>0,1)
“保持差异大于零的列”
答案 1 :(得分:1)
发布的答案都不正确。测试A
只有1行或2行的边缘情况:
即:
A = [1 2 3];
或:
A = [1 2 3;
2 3 3];
diff
和any
需要提供正确的维度:
A = A(:,any(diff(A,1,1),1));
输出:
A = [1 2 3; 2 3 3];
EDU>> A(:,any(diff(A,1,1),1))
ans =
1 2
2 3
和
A = [1 2 3]
EDU>> A(:,any(diff(A,1,1),1))
ans =
Empty matrix: 1-by-0
此外,IMO在语义上最有意义:
A(:,all(bsxfun(@eq,A,A(1,:)),1)) = []
答案 2 :(得分:0)
怎么样:
A =
1 2 3
2 3 3
3 4 3
3 5 3
B = A==repmat(A(1,:),size(A,1),1)
B =
1 1 1
0 0 1
0 0 1
0 0 1
C = sum(B) == size(A,1)
C =
0 0 1
A(:,C) =[]
A =
1 2
2 3
3 4
3 5
在一行中:
A(:, sum(A==repmat(A(1,:),size(A,1),1)) == size(A,1)) = []