How to save the leading zeroes to values

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..

 Risposta accettata

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 Commenti

Marcin Kolacz
Marcin Kolacz il 27 Mag 2019
Modificato: Marcin Kolacz il 27 Mag 2019
Hi, your solution also does not satisfy my needs. I plugged it in and that's what i got
Edit: the variable "res" is really nice, but the conversion into number recreates the problem
Meanwhile what I need is:
I generate myself a couple of random 0/1 values, one value in one place i.e. I have an array A = [1,1,1,0,1,0,0,1,0,0,1,0, [...] ] and so on
Now what i would like to do is to group all the values into groups of four so in this example array B = [1110,1001,0010, [...] ].
The problem is - when the first out of 4 bits is '0', then the first multiple zeros will be omitted and i get something like in the picture included (100 instead of 0100, 110 instead of 0110 or 11 instead of 0011 or 0 instead of 0000)
Well, you didn't make it clear what your "needs" were, specifically.
There is no solution other than displaying the character representation of the value with a '%0d' format or the like...numeric values are by default displayed with at '%g' format that will not show leading zeros.
For a numeric value, what the appearance externally is is immaterial; "the bits are the bits".
At this point think you'll have to explain what it is you're going to do with the data next that it matters...
Look at the output of the res variable; it will show the desired pattern but it is (by necessity) a character representation, NOT numeric, in order to show the leading zeros.
Of course,
>> sprintf('%04d\n',str2num(ans))
ans =
'0001
1011
0011
1100
1110
1110
1101
1010
'
>>
reproduces the output format...
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
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.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 27 Mag 2019

Commentato:

dpb
il 27 Mag 2019

Community Treasure Hunt

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

Start Hunting!

Translated by