All combinations of matrix elements?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I'd like to find all combinations of 2 elements in a matrix.
For example, a = [1:4; 5:8; 9:12]', the result should be [1*1 1*2 1*3 1*4 1*5 ... 1*12 2*2 2*3 ... 2*12 3*3 ... 12*11 12*12]';
Thanks for your help!
采纳的回答
Stephen23
2016-9-23
编辑:Stephen23
2016-9-27
Method One: matrix multiply and tril:
>> z = a(:) * a(:)'
>> z(tril(true(size(z))))
>> prod(a(combinator(numel(a),2,'c','r')),2)
5 个评论
Stephen23
2016-9-26
@Xiaohan Du: cell arrays are containers for other data classes. They cannot have mathematical operations applied to them. Whatever you want to do (it is not clear to me) needs to be applied to a numeric array, not a cell array.
James Tursa
2016-9-26
@Xiaohan Du: It is unclear what solution you really want from your original post. Stephen's answer does the entire outer product, which does not match your example result which appears to be only one triangular portion of the outer product. Andrei's answer does this triangular portion which matches your original post example result. What is it you really want? (The accepted answer does not match the question example result)
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!