필터 지우기
필터 지우기

How to delete columns of a matrix that have fewer than 3 non-zero elements

조회 수: 3 (최근 30일)
Hello,
How can I delete columns of a matrix that have fewer than 3 non-zero elements? Thank you!
  댓글 수: 4
osminbas
osminbas 2012년 3월 1일
Yes, that is what I meant. Fewer than 3 non-zero elements.
osminbas
osminbas 2012년 3월 1일
I can define it as matrix A is 5x5. Say two columns (column 1 and 3) have only 2 non-zero elements.

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

채택된 답변

Wayne King
Wayne King 2012년 3월 1일
Hi, I'm sure this isn't the most efficient way:
x =[
5 0 1 4 1
3 0 0 2 1
1 0 2 2 4
4 3 0 2 3
4 2 0 1 3];
for nn =1:size(x,2)
numz(nn) = length(find(x(:,nn)==0));
end
x(:,numz>2) = [];
  댓글 수: 1
osminbas
osminbas 2012년 3월 1일
Actually I did this:
for nn =1:size(x,2)
numz(nn)=length(find(x(:,nn)~=0));
end
x(:,numz<3)=[];
It worked! Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by