Shrink a 1-D array (vector) by removing all the columns with a value of zero

 채택된 답변

Here is a good solution:
NewSimpleArray = SimpleArray(SimpleArray ~= 0)

댓글 수: 4

For a good discussion of different solutions to this type of problem, see the Newsgroup thread here:
How would you do this in simulink?

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

추가 답변 (4개)

David
David 2012년 3월 20일

0 개 추천

Thanks for the answers and for showing me the previous discussion string (I didn't think this was the first time this question was asked)
seif seif
seif seif 2018년 1월 21일
편집: seif seif 2018년 1월 21일
Using nonzeros is also very simple (note that the output is a column vector):
NewSimpleArray = nonzeros(SimpleArray)
NewSimpleArray =
1
2
3
4
5

댓글 수: 2

That changes the shape from a row vector to a column vector. However it can be fixed with the code below:
SimpleArray = [1,0,2,0,3,0,4,0,5,0] % Row Vector
NewSimpleArray = nonzeros(SimpleArray) % Creates column vector.
% Reshape back into a row vector.
NewSimpleArray = reshape(NewSimpleArray, 1, [])
How about matrix?
What if the output is still a matrix after removing zero elements?

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2012년 3월 20일

댓글:

2019년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by