From: Leandro Callegari Coelho on
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
From: dpb on
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... :)
--
From: Matt Fig on
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'.

>>
From: Leandro Callegari Coelho on
On Apr 27, 8:55 am, "Matt Fig" <spama...(a)yahoo.com> wrote:
> 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'.
>
>

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

N=5
for i=1: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
From: Matt Fig on
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