This is my code:
N=3;
x=randi(9,3,N)
func = @(index)bsxfun(@minus,x(:,1:N),x(:,index));
mat = cell2mat(arrayfun(func,1:N,'uni',0))
This is my output:
0 -5 -4 5 0 1 4 -1 0
0 0 -6 0 0 -6 6 6 0
0 -2 5 2 0 7 -5 -7 0
How do I remove columns with zeros?

 채택된 답변

madhan ravi
madhan ravi 2019년 2월 10일
편집: Guillaume 2020년 4월 1일

5 개 추천

matrix(:,any(matrix == 0))=[] % removes column if there is any zero in the column
matrix(:,all(matrix == 0))=[] % removes column if the entire column is zero

댓글 수: 4

Shouldnt it be like this then?
matrix(:,any(matrix==0))=[] % removes column if there is any zero in the column
matrix(:,all(matrix==0))=[] % removes column if the entire column is zero
Guillaume
Guillaume 2020년 4월 1일
편집: Guillaume 2020년 4월 1일
@Jon, yes you are correct. I've fixed Madhan's answer (and accepted it)
madhan ravi
madhan ravi 2020년 4월 2일
Thank you Jon and Guillaume:)
Ömer BOZKURT
Ömer BOZKURT 2022년 8월 16일
Hello Sir. I have a question about delete columns. I want to delete columns if columns have more than 3 zero. How can I find it? Thanks for helping.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2019년 2월 10일

댓글:

2022년 8월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by