help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: transform a string with variable length to a matrix


From: Benjamin Lindner
Subject: Re: transform a string with variable length to a matrix
Date: Thu, 28 Aug 2008 11:57:28 +0200
User-agent: Thunderbird 2.0.0.14 (Windows/20080421)

Hi,

Using dec2bin I transform a decimal number to a binary number
with an a priori unknown number of digits. But the next step
offers a little problem for me: I have to transform the resulting
string to a matrix of ones and zeros. Trying a bit with str2mat
like str2mat([a(1);a(2);a(3)] wasn't successful 'cause the
number of digits is variable.


If I interpret your problem correctly, then you want to transfer a number into a vector containing the binary representation.
You can do this without the intermediate string conversion by

  A = 9;
  Nbits = ceil(log2(A));
  bitmap = uint32( 2.^[0:Nbits-1] );
  M = double(bitand(A,bitmap) ~= 0);

this will yield the 1x4 matrix M
  M = 1 0 0 1


Benjamin Lindner


reply via email to

[Prev in Thread] Current Thread [Next in Thread]