No BSD License  

Highlights from
int2vec

3.57143

3.6 | 7 ratings Rate this file 2 Downloads (last 30 days) File Size: 1.08 KB File ID: #8329

int2vec

by Jim Simos

 

21 Aug 2005 (Updated 22 Aug 2005)

Convert integer to vector.

| Watch this File

File Information
Description

Useful when you want to interpret integers as codewords.It also could be used to convert the output of the binary representation of an integer to vector.

Function call v=int2vec(s,l)
 s is the integer to be converted.
 l is the length of the integer in bits. For example v=int2vec(110,3)
 returns v=[1 1 0].

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
22 Aug 2005 Jos x@y.z

The following fast one-liner will produce the same result:
sprintf(sprintf('%%0%id',LEN),INT)-'0'
in which LEN is the length and INT is the integer. There many other vectorized ways as well.
And what if the input is 120 ??

22 Aug 2005 Jim Simos

It certainly needs improvement.
Btw,your suggestion fails from 11 bits. :)

>> vec=sprintf(sprintf('%%0%id',11),12345678912)-'0'

vec =

     1 -2 2 3 4 5 6 8 53 -5 0 1 0

22 Aug 2005 urs (us) schwarz

Btw,your suggestion fails from 11 bits...
...because jos really meant to say

num=1234567890123456;
len=20;
v=sprintf(sprintf('%%0%d.f',len),num)-'0'

us

23 Aug 2005 Patrick Weber

Good,enough.More convenient to use than the sprintf command.

06 Sep 2005 Tao Xia

Good,but simple

04 Oct 2005 Jos Wiley

Excellent!Helped me a lot.Thanks.

12 Dec 2005 Valtteri Ervasti

Thank you! Just what I needed.

14 Dec 2005 Andrea Svirkokakhova

Very helpful!

08 Feb 2006 evgeni evshenko

what about
str2num((num2str(n)'))'
where n is any integer?

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
coding theory Jim Simos 22 Oct 2008 07:56:19
information theory Jim Simos 22 Oct 2008 07:56:19
coding and information theory Jim Simos 22 Oct 2008 07:56:19
convert integer Jim Simos 22 Oct 2008 07:56:19
vector Jim Simos 22 Oct 2008 07:56:19
interpret Jim Simos 22 Oct 2008 07:56:19

Contact us at files@mathworks.com