How can I replace integer values in a array?

I Have some values in a array like this.
A = -4
-4
-4
4
4
4
I want to convert all negative values to 0 and all positive values to 1.
How can I do that.??
Outcome should be :
B = 0
0
0
1
1
1

답변 (2개)

Matt J
Matt J 2021년 1월 3일
편집: Matt J 2021년 1월 3일

0 개 추천

B=(sign(A)+1)/2

댓글 수: 1

William
William 2021년 1월 3일
... but be aware that sign(0)=0, so B becomes 0.5 in this case.

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

카테고리

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

제품

릴리스

R2014b

질문:

2021년 1월 3일

답변:

2021년 1월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by