problem with subsets

  • Follow


Hi everyone,

I'm struggling to write a set of equations. Suppose I have the vector
N={1  2 3 ... 10}
and I write the loop

for i=1:1:N
   fprintf(' + x%d ', i);
end
fprintf(' = 1');

But now I need to write the same equation for all possible subsets of
N, ranging from 2 to 8 elements.
So it can be only elements {1, 2}, {1, 3}, ... {5, 10}, up to all
combinations until 8 elements.

Any ideas of hints are very welcome.
Thnaks
0
Reply leandro.cc (1) 4/27/2010 4:36:43 PM

Leandro Callegari Coelho wrote:
> Hi everyone,
> 
> I'm struggling to write a set of equations. Suppose I have the vector
> N={1  2 3 ... 10}
> and I write the loop
> 
> for i=1:1:N
>    fprintf(' + x%d ', i);
> end
> fprintf(' = 1');
> 
> But now I need to write the same equation for all possible subsets of
> N, ranging from 2 to 8 elements.
> So it can be only elements {1, 2}, {1, 3}, ... {5, 10}, up to all
> combinations until 8 elements.
> 
> Any ideas of hints are very welcome.

Consider the results of the following...

for idx=1:N
   nchoosek(1:N,idx)
end

You might find w/ N rather small first more easily enlightening... :)
--
0
Reply dpb 4/27/2010 4:47:56 PM


You need to give an example which doesn't fail.  Perhaps a simpler version of your actual problem, with the inputs and outputs shown explicitly, would help.


>> N = {1 2 3 4 5 6 7 8 9 10};
>> for i=1:1:N
   fprintf(' + x%d ', i);
end
fprintf(' = 1');
??? Undefined function or method '_colonobj' for input arguments of type 'cell'.

>> 
0
Reply Matt 4/27/2010 4:55:08 PM

On Apr 27, 8:55=A0am, "Matt Fig" <spama...@yahoo.com> wrote:
> You need to give an example which doesn't fail. =A0Perhaps a simpler vers=
ion of your actual problem, with the inputs and outputs shown explicitly, w=
ould help.
>
> >> N =3D {1 2 3 4 5 6 7 8 9 10};
> >> for i=3D1:1:N
>
> =A0 =A0fprintf(' + x%d ', i);
> end
> fprintf(' =3D 1');
> ??? Undefined function or method '_colonobj' for input arguments of type =
'cell'.
>
>

Matt, here's a simple version (believe me, the equations I'm trying to
write are much more complex than this).

N=3D5
for i=3D1:1:N
fprintf(' + x%d', i);
end

This will write:
x1 + x2 + x3 + x4 + x5

Now I want something to write approx 2^N equations, the output would
be

x1 + x2
x1 + x3
x1 + x4
x1 + x5
x2 + x3
....
than w/ 3 elements:
x1 + x2 + x3
x1 + x2 + x4
x1 + x2 + x5
....

If N is 10, than I need all possible combinations from 2 to 8
elements(2 to (N-2) elements).

I'll try with dbp's suggestion (thanks!)

Regards
0
Reply Leandro 4/27/2010 5:20:44 PM

Something like this:



N = 10;

for jj = 2:8
    T = nchoosek(1:N,jj);

    for ii = 1:size(T,1)
        fprintf(['x%i',repmat(' + x%i',1,size(T,2)-1),'\n'],T(ii,:))
    end
    fprintf('\n\n\n')
end
0
Reply Matt 4/27/2010 5:34:05 PM

On Apr 27, 9:34=A0am, "Matt Fig" <spama...@yahoo.com> wrote:
> Something like this:
>
> N =3D 10;
>
> for jj =3D 2:8
> =A0 =A0 T =3D nchoosek(1:N,jj);
>
> =A0 =A0 for ii =3D 1:size(T,1)
> =A0 =A0 =A0 =A0 fprintf(['x%i',repmat(' + x%i',1,size(T,2)-1),'\n'],T(ii,=
:))
> =A0 =A0 end
> =A0 =A0 fprintf('\n\n\n')
> end

Matt, thanks a lot. It works, I just need to adapt it to my equations.
I really appreciate your attention.
0
Reply Leandro 4/27/2010 5:52:16 PM

Leandro Callegari Coelho wrote:
> On Apr 27, 9:34 am, "Matt Fig" <spama...@yahoo.com> wrote:
>> Something like this:
>>
>> N = 10;
>>
>> for jj = 2:8
>>     T = nchoosek(1:N,jj);
....
> 
> Matt, thanks a lot. It works, I just need to adapt it to my equations.
> I really appreciate your attention.

for jj = 2:8
      T = nchoosek(1:N,jj);

looks somehow remarkably similar to dpb's hint to explore

for idx=1:N
   nchoosek(1:N,idx)

:)

--

0
Reply dpb 4/27/2010 8:25:22 PM

dpb <none@non.net> wrote in message <hr7hat$7kd$1@news.eternal-september.org>...
> Leandro Callegari Coelho wrote:
> > On Apr 27, 9:34 am, "Matt Fig" <spama...@yahoo.com> wrote:
> >> Something like this:
> >>
> >> N = 10;
> >>
> >> for jj = 2:8
> >>     T = nchoosek(1:N,jj);
> ...
> > 
> > Matt, thanks a lot. It works, I just need to adapt it to my equations.
> > I really appreciate your attention.
> 
> for jj = 2:8
>       T = nchoosek(1:N,jj);
> 
> looks somehow remarkably similar to dpb's hint to explore
> 
> for idx=1:N
>    nchoosek(1:N,idx)
> 
> :)
> 
> --
I went with 
N=[1 2 3 4];
len=length(N);
for i =0:1:2^(len)-1
 subset=dec2bin(i,len)=='1';
 for j= N(subset)
    fprintf(' + x%d ', j);
 end
 fprintf('=1\n');
end

if you don't want the empty set (ie =1) just switch the loop over i to start with 1
I also changed N from a struct to a vector
0
Reply Adam 4/27/2010 8:59:07 PM

Guys, since you're posting here again, I'll ask your help once again.
Turns out the equation I'm trying to write is way too hard to me.

Take a look at the equation here: http://twitpic.com/1iy2bu
Using your ideas I could almost finish the left side.

Explaining from the right to the left:
Big-fancy t can be small, i.e 3
Big-fancy M is what we're using N here, so it can be something like 5.

(for the ones that are curious this equation avoids subtours on a
vehicle routing model)

Anyone with enough time to tackle this?
0
Reply Leandro 4/27/2010 9:35:01 PM

On Apr 27, 1:35=A0pm, Leandro Callegari Coelho <leandro...@gmail.com>
wrote:
> Guys, since you're posting here again, I'll ask your help once again.
> Turns out the equation I'm trying to write is way too hard to me.
>
> Take a look at the equation here:http://twitpic.com/1iy2bu
> Using your ideas I could almost finish the left side.
>
> Explaining from the right to the left:
> Big-fancy t can be small, i.e 3
> Big-fancy M is what we're using N here, so it can be something like 5.
>
> (for the ones that are curious this equation avoids subtours on a
> vehicle routing model)
>
> Anyone with enough time to tackle this?

bump
0
Reply Leandro 4/28/2010 12:44:22 PM

9 Replies
352 Views

(page loaded in 0.133 seconds)

Similiar Articles:













7/26/2012 10:46:31 PM


Reply: