COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### dec2bin modification

• Email
• Follow

```Dear all goodmorning,

I have a question related to dec2bin.

I am currently using the following in one of my programs to create a
matrix with all the possible 0-1 combinations for a value of n. This
results in a '2^n by n' matrix.

n=3
A=(dec2bin(0:(2^n)-1)=='1');
A

Result
------

A =

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

What I really need is to introduce another variable, say m=2, and
modify the dec2bin command somehow in order for it to produce a
matrix with only the 0-1 combinations for the value of n, that
contain so many 1s equal to m.

So for example, the new matrix based on the example above should be:

A =

0 1 1
1 0 1
1 1 0

because only these 3 lines contain a 0-1 combination with the number
of 1's equal to m (m=2).

I am currently using the standard dec2bin producing all the possible
combinations, and then I use a 'for' loop that checks every signle
line, does the sum of the line, and if it is greater or less than the
value of m it deletes the line from the matrix. It works ok but it is
very time-consuming.

I was hoping for some modification on the original dec2bin command to
get it to bring ONLY the combinations with the number of 1's equal to
m.

Regards,
```
 0

See related articles to this posting

```P� Thu, 17 Mar 2005 01:58:30 -0500, skrev Antonios
<antonios.tatarakis@gmail.com>:

> n=3
> A=(dec2bin(0:(2^n)-1)=='1');
> A
>
> Result
> ------
>
> A =
>
>      0 0 0
>      0 0 1
>      0 1 0
>      0 1 1
>      1 0 0
>      1 0 1
>      1 1 0
>      1 1 1
>
<reduce to>
>
> A =
>     0 1 1
>      1 0 1
>      1 1 0

You dont have to use a loop

B=A(sum(A,2)==2,:)

will pick all rows with sum==2

--
K
```
 0
Reply firstname1 (225) 3/17/2005 9:26:57 AM

```Kristian,

thank you very much for the tip.
Did not think of the simple solution ;-)

A
```
 0

```Antonios wrote:
>
>
<snip, ones and zeros problem...

Another solution, not using dec2bin:

>> n=3;
>> m=2;
>> unique(perms([ones(1,m),zeros(1,n-m)]),'rows')

ans =

0 1 1
1 0 1
1 1 0
```
 0
Reply Firstname.Lastname4194 (1680) 3/17/2005 4:21:26 PM

```In article <eeff439.-1@webx.raydaftYaTP>, Antonios
<antonios.tatarakis@gmail.com> wrote:

> Dear all goodmorning,
>
> I have a question related to dec2bin.
>
> I am currently using the following in one of my programs to create a
> matrix with all the possible 0-1 combinations for a value of n. This
> results in a '2^n by n' matrix.
>
> n=3
> A=(dec2bin(0:(2^n)-1)=='1');
> A
>
> Result
> ------
>
> A =
>
>      0 0 0
>      0 0 1
>      0 1 0
>      0 1 1
>      1 0 0
>      1 0 1
>      1 1 0
>      1 1 1
>
> What I really need is to introduce another variable, say m=2, and
> modify the dec2bin command somehow in order for it to produce a
> matrix with only the 0-1 combinations for the value of n, that
> contain so many 1s equal to m.
>
> So for example, the new matrix based on the example above should be:
>
> A =
>
>      0 1 1
>      1 0 1
>      1 1 0
>
> because only these 3 lines contain a 0-1 combination with the number
> of 1's equal to m (m=2).
>
> I am currently using the standard dec2bin producing all the possible
> combinations, and then I use a 'for' loop that checks every signle
> line, does the sum of the line, and if it is greater or less than the
> value of m it deletes the line from the matrix. It works ok but it is
> very time-consuming.
>
> I was hoping for some modification on the original dec2bin command to
> get it to bring ONLY the combinations with the number of 1's equal to
> m.
>
>
> Regards,
---------------------
Hello Antonios,

I found your problem interesting, so I wrote the accompanying function
which loops only through the nCm combinations of m 1-bits among n, instead
of creating larger arrays and then reducing them.  It is possible that for
sufficiently large values of n it would execute faster than the other
algorithms since it is order nCm rather than order 2^n or n!, but I cannot
test it fully on my Student Edition matlab with its 8192-element array
restriction.  In any case, feel free to use it if you like.  I've checked
it out as far as n = 11 and it appears to function properly.

%----------------
function x = bincomb(n,m);

% This generates all possible binary numbers
% with m ones and n-m zeros as rows in the
% nCm x n matrix x, where nCm is the number of
% combinations of m elements out of a set of
% n elements.  The binary numbers occur in x
% in ascending order.
% RAS - 3/17/05

% Check on n and m
if n~=floor(n)|m~=floor(m)|m<0|m>n
error('Improper arguments')
end

np1 = n + 1; mp1 = m + 1;
t = [1 zeros(1,n)];
for i = 1:n  % Generate Pascal's triangle
t = t + [0 t(1:n)];
end
nCm = t(mp1); % Obtain the number of combinations
x = zeros(nCm,n);  % Allocate space for x
if n==0, return, end % Quit early if n==0
y = [1 zeros(1,n-m) ones(1,m)]; % Start with ones on the right
for i = 1:nCm  % Loop once for each combination
x(i,:) = y(2:np1); % Add next row
t = find(y); n1 = t(mp1); % Find rightmost one
t = find([1 ~y(2:n1-1)]); n0 = t(n1-m); % Find next left zero
y = [y(1:n0-1) 1 0 y(n1+1:np1) y(n0+2:n1)]; % Prepare next row
end
%----------------

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
```
 0
Reply ellieandrogerxyzzy (4805) 3/18/2005 4:41:59 AM

```P� Thu, 17 Mar 2005 11:21:26 -0500, skrev Steve Amphlett
<Firstname.Lastname@where_I_work.com>:

> Antonios wrote:
>>
>>
> <snip, ones and zeros problem...
>
> Another solution, not using dec2bin:
>
>>> n=3;
>>> m=2;
>>> unique(perms([ones(1,m),zeros(1,n-m)]),'rows')
>
> ans =
>
>      0 1 1
>      1 0 1
>      1 1 0

I also had gave perms a go, but as stated in the help text: "for N=11, the
output takes over 3 giga-bytes". And if n is that small, the looping can't
take too long.

I think perms should have 'unique' as an input..

--
K
```
 0
Reply firstname1 (225) 3/18/2005 8:27:25 AM

5 Replies
28 Views

Similar Articles

12/9/2013 11:55:08 PM
[PageSpeed]

Similar Artilces:

IEEEtran.bst modification
Hi, I need to modify my IEEEtran.bst file for a journal submission to generate following type of reference A. B. Cdef and X. Yz, "Something, " IEEE Transactions on Computers (2003), Vol. xx, No yy, pp.1--10. the year 2003 must be in boldface The IEEEtran produces the following A. B. Cdef and X. Yz, "Something, " IEEE Transactions on Computers, vol. xx, no yy, pp.1--10, 2003. I just need a to change the position of the year from the end to the one next to the journal name. The year should be boldfaced and it should be within simple brackets. Thanks Anand On 1 Feb 200

program modification
in Streamline Your Life from MSN Money. http://special.msn.com/money/0405streamline.armx Jian: Is this outsourcing to SAS-L? If so, you'll have to complete and submit our new 28 page System Modification and Validation Request Form and submit it to the list for approval. Sig -----Original Message----- From: Jian Mao [mailto:maojianj@HOTMAIL.COM] Sent: Tuesday, June 01, 2004 12:13 AM To: SAS-L@LISTSERV.UGA.EDU Subject: program modification Hi, For the following program sent to me a while ago, I just want to add one thing, old rule: /* 2. After the 1st of occurrence of "1"... To: SAS-L@LISTSERV.UGA.EDU Subject: program modification Hi, For the following program sent to me a while ago, I just want to add one thing, old rule: /* 2. After the 1st of occurrence of "1" (remission=yes), follow up for 2 more periods. If the value at 2nd follow-up period=0, relapse=yes; otherwise, relapse=no. */ new rule: /* 2. After the 1st of occurrence of "1" (remission=yes), follow up for 2 more periods. If the value at THE FIRST PERIOD, or 2nd

If excel file, say temp.xls, has data like this, 'A' 10 'B' 20 and if I use [dNum, dTxt] = xlsread('temp.xls'); It gives me dNum = [10;20] I want MATLAB to give me [nan 10; nan 20]. I tried to use a function which overwrites MATLAB but it gets complicated. Anybody knows easier way to solve this problem. I checked xlsread function apparently it has data in the format that I need but it later chops NaNs from the first columns. I don't want to changed default xlsread. Any solutions will be appreciated. Chaos Chaos <achordedrose@gmail.com> wrote: >

Change directory modification date
Hello I need to change the modification date on a directory to 01-JAN-2006 I know you can change the modfication time on files using touch but I wondered if there was an equivilant for directories Thanks in advance For me touch also works with directories. I have bash on cygwin \$ ls -ld test drwxrwxrwx+ 2 18035 mkgroup-l-d 0 Apr 4 12:40 test \$ touch -d yesterday test \$ ls -ld test drwxrwxrwx+ 2 18035 mkgroup-l-d 0 Apr 3 12:40 test \$ \$0 --version GNU bash, version 3.2.33(18)-release (i686-pc-cygwin) Copyright (C) 2007 Free Software Foundation, Inc. Thanks for that I was using the wrong syntax with touch for the directories. It has worked after all!! In article <90201576-dcfd-4b59-8457-dd48e8be2cc5@24g2000hsh.googlegroups.com>, Florian Kaufmann <sensorflo@gmail.com> wrote: >For me touch also works with directories. I have bash on cygwin > >\$ ls -ld test >drwxrwxrwx+ 2 18035 mkgroup-l-d 0 Apr 4 12:40 test >\$ touch -d yesterday test >\$ ls -ld test >drwxrwxrwx+ 2 18035 mkgroup-l-d 0 Apr 3 12:40 test >\$ \$0 --version >GNU bash, version 3.2.33(18)-release (i686-pc-cygwin) >Copyright (C) 2007 Free Software Foundation, Inc. > >

Jar modification question
I need to be able to replace a default resource file with a site-specific one. Currently I'm doing the job with a bash script that builds the replacement resource file and then runs jar in update mode to put the new resource into the jarfile. This works and works well, but isn't particularly portable, so I looked into writing a Java utility, but the java.util.jar and/or java.util.zip packages don't seem to be able to replace files in an existing jar file. I've thought of a few other approaches using portable scripting tools: - Python: but not all machines have

Modification of Variable in NDSolve
I need to modify a variable while a system of ODE's are being solved. I've simplified the problem to the following: dA = B[t]; dB = (1 - A[t]^2) B[t] - A[t]; dF = B[t] + 0.2; Module[{count = 0, last = 0.}, sol = NDSolve[{A'[t] == dA, B'[t] == dB, F'[t] == dF, A[0] == 1, B[0] == 1, F[0] == 1}, {A, B, F}, {t, 0, 100}, StepMonitor :> If[last <= 5 <= F[t] && last < F[t], count++; last = F[t], last = F[t]] ]; count] This does a threshold crossing of 5 for the variable F from below. The count that results is correct, but

Modification to /etc/crontab #2
Hello, When I make a modification to /etc/crontab, the new command inserted is not taken into account until crond is restarted. Though, I do have the line (*system*) RELOAD (/etc/crontab) in /var/log/cron indicating that the file has been re-read. Here is the command : 21 11 * * * root echo HELLO > /dev/pts/0 Here is the log : Aug 14 11:21:01 h91-232-72 crond[14897]: (*system*) RELOAD (/etc/crontab) but nothing's echo'ed. If I modify the command to : 22 11 * * * root echo HELLO > /dev/pts/0 and then restart crond, I get : Aug 14 11:21:47 h91-232-72 crond[15460]: (CRON) STARTUP (V5.0) Aug 14 11:22:01 h91-232-72 crond[15475]: (root) CMD (echo HELLO > /dev/pts/0) and HELLO is printed to my terminal. Has anyone experienced this behaviour ? RedHat Fedora 2.6.19.7 vixie-cron-4.1-33 Thanks Christian In article <g80tqc\$d5d\$1@writer.imaginet.fr>, Christian <cgregoir99@yahoo.com> wrote: :Hello, : :When I make a modification to /etc/crontab, the new command inserted is not :taken into account until crond is restarted. Though, I do have the line : (*system*) RELOAD (/etc/crontab) :in /var/log/cron indicating

Modification of Duty Cycle
Hello all, My input clock has 50% duty cycle and 40 MHz. I want to have output clock of 25% duty cycle and 10MHz. Please let me know if anybody knows the way to go. Thanks, Drew Drew wrote: > My input clock has 50% duty cycle and 40 MHz. I want to have output > clock of 25% duty cycle and 10MHz. Please let me know if anybody knows > the way to go. Sounds like a simple counter running on your 40 MHz clock, running from 0 to 3, and decoding that as the output clock (1 vs 3) to get your 25% duty cycle. Something like: PROCESS VARIABLE cnt : integer RANGE 0 to 3 := 0; BEGIN WAIT

TG3 driver modification
Hi, I was wondering if it's possible to modify TG3 driver in order to prevent adapter from using on-board SRAM, and use system RAM instead. I have laptop with Broadcom NetLink (BCM57780) adapter under Ubuntu 12.04. After plugging in the cable, adapter establishes connection with router, connects to Internet for few seconds and then the whole system completely freezes. It's doing basically the same under Windows. I have used Broadcom's utility to test adapter, and the result indicates on-board SRAM is faulty ( http://i.imgur.com/MExTE.jpg ). Cheers, SB

select file by modification time
Hello, I would like to be able to select a file by its modification time. For instance, if I have 3 files html files with different modification times, I can sort them according to modification times so that the order goes from least recent to most recent: ls -lrt *.html -rw-r--r-- 1 don users 12 Jan 2 11:56 zzz.html -rw-r--r-- 1 don users 14 Jan 2 11:57 aaa.html -rw-r--r-- 1 don users 12 Jan 2 12:05 bbb.html This is fine, but what I'd really like to do is select only the first file (zzz.html) having the least recent modification time. I figure this requires piping the above command into another command, but so far I haven't figured out which command does the job. Any suggestions are welcome. -- Thanks, Don >>>>> "Don" == Don Low <m_tessier@sympatico.ca> writes: Don> This is fine, but what I'd really like to do is select only the first Don> file (zzz.html) having the least recent modification time. newest=`ls -rt | head -1` -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn