From: esozo on
Hi there,

I need to use each pair of 3 columns of a variable, 1-2, 1-3, 2-3 (fancy it's called combination) as inputs in a two-input function. Any idea how to get that into a for loop, if that is a good idea at all? If something like this was already discussed out there, I must have missed the right keywords. Thanks for helping.
From: ImageAnalyst on
What is the variable? Is it a string with texts like '1-2' etc.? Or
is it a 2 by 3 numerical array? Or is it a cell array? Give some
code to create your sample data that people can use to try a little
programming on.
From: esozo on
> What is

Sorry, my wording was unclear, I should ould have said "matrix" or "array" instead of "variable". The question is how to get a pair of indexes in a for loop that are obtained by combination. E.g. for 1, 2 and 3 you have pairs 1-2, 1-3, 2-3.

% or if you want
M = rand(some dimension,3);
for each of the pairs 1st and 2nd column, 1st and 3rd, 2nd and 3rd column of M
do somefunction(x,y,...)
end

Hope it is now more clear. If someone could give a generalized idea (for N instead for 3), even much better. Thanks.
From: Bruno Luong on
"esozo " <esozo001(a)aol.de> wrote in message <hvct7s$8r6$1(a)fred.mathworks.com>...
> > What is
>
> Sorry, my wording was unclear, I should ould have said "matrix" or "array" instead of "variable". The question is how to get a pair of indexes in a for loop that are obtained by combination. E.g. for 1, 2 and 3 you have pairs 1-2, 1-3, 2-3.
>
> % or if you want
> M = rand(some dimension,3);
> for each of the pairs 1st and 2nd column, 1st and 3rd, 2nd and 3rd column of M
> do somefunction(x,y,...)
> end
>
> Hope it is now more clear. If someone could give a generalized idea (for N instead for 3), even much better. Thanks.

You could generate all combination with

N=10
c=nchoosek(1:N,2)

Then perform a single loop on rows of c.

Bruno
From: esozo on
> c=nchoosek(1:N,2)
> Then perform a single loop on rows of c.

Thanks, this works somehow.

M = rand(somedimension, N);
c = nchoosek(1:N, 2);
myoutput = arrayfun( @(r) somefunction(M(:,c(r,1)), M(:,c(r,2))), 1:size(c,1), 'Unif', true);