convert each character of an array to int

조회 수: 3 (최근 30일)
Mireia Boneta Camí
Mireia Boneta Camí 2020년 10월 24일
댓글: Ameer Hamza 2020년 10월 24일
I have a char array like this '10000111101010110101' and I want to convert each 0 and 1 to int and obtain [1 0 0 0 0 1 1 ...]. I'd like to do it without fors, is it possible?

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 24일
Easy peasy
x = '10000111101010110101';
y = x - '0'
  댓글 수: 2
drummer
drummer 2020년 10월 24일
how does this work?
Ameer Hamza
Ameer Hamza 2020년 10월 24일
If you look at the ASCII code for the character, you can see what is happening.
For example,
x = '10000111101010110101';
y = x + 0
Result
>> y
y =
Columns 1 through 17
49 48 48 48 48 49 49 49 49 48 49 48 49 48 49 49 48
Columns 18 through 20
49 48 49
So, if you subtract the ascii code for '0', you will get actual numbers.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by