Capturing non-zeros elements in matrix

Let's say we have a matrix A.From matrix A, I want to remove zeros and create another elements,capturing only non-zeros values. Can you please show me how to perform the above operation.

답변 (3개)

Jan
Jan 2011년 2월 27일

1 개 추천

A = [0 2 3; 4 3 0]
B = A(A ~= 0);
Now B is a vector, not a matrix anymore.

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 2월 28일
Or if conciseness is more important than speed:
B = A(~~A)

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

Andreas Goser
Andreas Goser 2011년 2월 27일

0 개 추천

A=[0 2 3; 4 3 0]
find(A)

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 2월 28일
That would give you the locations of the non-zero elements, but would not in itself create a new array with the non-zero elements. Similar to Jan's answer, this would have to be extended to
B = A(find(A))
Jan
Jan 2011년 2월 28일
@Walter: Of course. I'm sure that Andreas believes in the power of the OP to find this obvious solutionby himself.

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

Walter Roberson
Walter Roberson 2011년 2월 28일

0 개 추천

B = nonzeros(A);
The result will be a column vector.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by