How can treat with a 0,1 number as a binary?!

조회 수: 3 (최근 30일)
nalja mot
nalja mot 2018년 8월 26일
댓글: Image Analyst 2018년 8월 27일
Hi
could anyone please help me? I want to define A as a integer number:101011, but next i need to treat with this number as a binary format! for example i want get not from it! imagine i have D=[1 1 1 0 1 0 0 1 1 0 1] an i extract A=D(7:11)=01101 and want to get A_not=10010 but because of the integer nature of D, i can not do this! how can i do?
  댓글 수: 3
nalja mot
nalja mot 2018년 8월 27일
편집: nalja mot 2018년 8월 27일
it is string i have a mistake in this explanation assume with some instructions i got a single number:01101 from vector D=[1 1 1 0 1 0 0 1 1 0 1]
now i want to get not from it!
let me explain it with details: i have a vector of integer number(but this vector consist just 0 and 1):D=[1 1 1 0 1 0 0 1 1 0 1]
i want to extract some elements of it as a single number: A=D(7:11)=...=01101
lots of instructions exist to get a single number as A=D(7:11)=...=1101
for example:
x = [0 0 1 0 1 0 1];
p = flipud([1; cumprod(repmat(10, size(x, 2) - 1, 1))]);
y = x * p;
this instruction results a single integer number as:
y=10101
but i want to get 0010101 and i can get 1101010(as a single integer number) with do little changes in this instructions i think if i can get not of this result so i have 0010101 but because it is integer, "not" of this, is "0"!!
so i want to get 0010101 (as a binary single number albeit if i get exactly a single number 0010101 it is not important that it is binary or integer because i want to compare it with a constant that can define it binary or integer, but i think it is impossible that i get 0010101 as integer!!)
Image Analyst
Image Analyst 2018년 8월 27일
You cannot get leading zeros unless y is a string. It can't be a logical data type, integer data type, or double data type and have leading zeros unless you use fprintf() or sprintf() to display it as a string.

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

답변 (1개)

Image Analyst
Image Analyst 2018년 8월 26일
You can use the logical() function.
D = [1 1 1 0 1 0 0 1 1 0 1]
A = logical(D(7:11))
A_not = ~A

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by