find all the possible pairs with postive elements from a given matrix

Hi everybody, I have a specefic Problem to solve which a little bit complicated. lets say i have a 3*3 matrix(rows are the outputs and columns are the inputs ) with negative and positive values.
example:
A= [0.1 -1.5 -0.1;
-5.2 1.7 1.8;
2.9 -3.1 0.1]
so the possible pairs will be :
A(1,1) A(2,2) A(2,3)
A(1,1) A(2,2) A(3,3)
A(3,1) A(2,2) A(2,3)
A(2,1) A(2,2) A(3,3)
so that every inputs become an output with the condition of the positive coresponding element.
any propositions or keywords or functions to solve this specific problem ?

 Accepted Answer

t = A > 0;
k = (1:size(A,1))'.*t;
p = arrayfun(@(x)k(k(:,x) > 0,x),1:size(A,2),'un',0);
jj = cellfun(@numel,p);
ii = flip(fullfact(flip(jj,2)),2);
out = A(t);
out = out(ii+[0,cumsum(jj(1:2))])

More Answers (2)

Another way to open the same result:
A = [0.1 -1.5 -0.1;
-5.2 1.7 1.8;
2.9 -3.1 0.1];
B = cellfun(@(col) col(col>0), num2cell(A, 1), 'UniformOutput', false);
[B{:}] = ndgrid(B{:});
B = reshape(cat(4, B{:}), [], 3)

1 Comment

Super! (my favorite tools), +1! Small edit:
B = cellfun(@(col) col(col>0), num2cell(A, 1), 'UniformOutput', false);
[B{end:-1:1}] = ndgrid(B{end:-1:1});
B = reshape(cat(4, B{:}), [], 3);

Sign in to comment.

It looks like you want triplets, or I don't understand the question:
>> posUnique = unique( A(A > 0) )
posUnique =
0.1000
1.7000
1.8000
2.9000
>> ids = nchoosek( 1:numel(posUnique), 3 )
ids =
1 2 3
1 2 4
1 3 4
2 3 4
>> B = Apos(ids)
B =
0.1000 2.9000 1.7000
0.1000 2.9000 1.8000
0.1000 1.7000 1.8000
2.9000 1.7000 1.8000

4 Comments

not really ! i have a system with 3 inputs and 3 outputs. the matrix A contains values wich they will be used to determine the pairs. the condition is positive values. in your solution was
0.1 2.9 1.8
and 0.1 2.9 1.8
-> this 2 combinations are not possible because 0.1 and 2.9 are elements for the same input u1 the solution will be:
0.1 1.7 1.8
0.1 1.7 0.1
2.9 1.7 1.8
2.9 1.7 0.1
as you see for the 2nd column was always 1.7 becuse for in the second column was only one positive value. otherwise formulated we it is possible only to pick one value of each value
I hope i could explain...
[r,c] = find(A > 0) ;
ig = splitapply(@(x){x}, r, c) ;
vg = splitapply(@(x){x}, A(sub2ind(size(A), r, c)), c) ;
vol = ones(cellfun(@numel, ig ).') ;
[i1,i2,i3] = ind2sub( size(vol), find(vol)) ;
B = [vg{1}(i1),vg{2}(i2),vg{3}(i3)]
I'm voting for Andrei! Next time I'll remember FULLFACT instead of creating a volume ..

Sign in to comment.

Categories

Find more on Signal Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!