How to save the leading zeroes to values

조회 수: 1 (최근 30일)
Marcin Kolacz
Marcin Kolacz 2019년 5월 27일
댓글: dpb 2019년 5월 27일
Hello, I really struggle to find an answer.. I have a loop that takes every 4 bits (0/1 values), groups them into one value and then stores them in another variable. The problem is that if one value finishes, and the next one starts with '0' it's being omitted. How can i fix this?
max = 1024;
a = randsrc(1, max, 0:1);
for i=1:100
result = strcat(num2str(a(4*i-3)),num2str(a(4*i-2)),num2str(a(4*i-1)),num2str(a(4*i)));
pom(i)=str2num(result)
end
I tried to make something like this
if(a(4*i-3)==0)
b = 0;
resuddlt = strcat(num2str(3),num2str(a(4*i-2)),num2str(a(4*i-1)),num2str(a(4*i)));
else
resuddlt = strcat(num2str(a(4*i-3)),num2str(a(4*i-2)),num2str(a(4*i-1)),num2str(a(4*i)));
end
And when I use any other number than 0 it works, but with 0 it does not and is omitted again..

채택된 답변

dpb
dpb 2019년 5월 27일
res=reshape(sprintf('%d',a),4,[]).';
pom=str2num(res);
to produce the decimal representation of the bit pattern. Is this what you really want?
Show your expected result for a given (short) input vector so we can tell when we have the right solution--the crystal ball isn't working today; I don't have a reading... :)
  댓글 수: 4
Marcin Kolacz
Marcin Kolacz 2019년 5월 27일
Well, I need those to process them further into signal modulation - these will be my bits that I need to transmit - signal processing is the thing.. somehow I need to operate on such values
dpb
dpb 2019년 5월 27일
Well, the numeric value is the numeric value, so operate on it.
If you need the actual character sequence, then use it instead.
You've not yet given enough info on what that processing step expects for input to know but "bits is bits!" so my first guess is it doesn't matter.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by