decimal to binary conversion of an array

조회 수: 2 (최근 30일)
PRAVEEN GUPTA
PRAVEEN GUPTA 2019년 10월 1일
답변: PRAVEEN GUPTA 2019년 10월 2일
i have an array a=[102, 2,3,201,8,9]
so when i convert it to binary each term has same length as
[ 0 1 1 0 0 1 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1
1 1 0 0 1 0 0 1
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 1]
but i want each term to become binary according to the size not with padded zero as 2 in binary is '10' not '0 0 0 0 0 0 1 0'
thanks in advance
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 10월 2일
Numeric only
Sorry, that is not possible in MATLAB. In MATLAB, numeric arrays must always have the same number of columns in each row.
If you had wanted cell array of character vectors, or string array, or cell array of numeric vectors, then those would be possible.
PRAVEEN GUPTA
PRAVEEN GUPTA 2019년 10월 2일
Then how to do cell array of numeric vector??

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

채택된 답변

Stephen23
Stephen23 2019년 10월 2일
편집: Stephen23 2019년 10월 2일
Cell array of character vectors:
>> a = [102,2,3,201,8,9];
>> c = arrayfun(@dec2bin,a,'uni',0);
>> c{:}
ans = 1100110
ans = 10
ans = 11
ans = 11001001
ans = 1000
ans = 1001
Cell array of numeric vectors:
>> c = arrayfun(@(n)dec2bin(n)-'0',a,'uni',0);
>> c{:}
ans =
1 1 0 0 1 1 0
ans =
1 0
ans =
1 1
ans =
1 1 0 0 1 0 0 1
ans =
1 0 0 0
ans =
1 0 0 1

추가 답변 (1개)

PRAVEEN GUPTA
PRAVEEN GUPTA 2019년 10월 2일
Thanks

카테고리

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