All combinations of matrix elements?

조회 수: 2 (최근 30일)
Xiaohan Du
Xiaohan Du 2016년 9월 23일
편집: Stephen23 2016년 9월 27일
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
Stephen23 2016년 9월 23일
편집: Stephen23 2016년 9월 27일
Method One: matrix multiply and tril:
You can use a simple matrix multiply to do this:
>> z = a(:) * a(:)'
and use tril to get the triangular lower portion:
>> z(tril(true(size(z))))
Method Two: FEX submission combinator:
>> prod(a(combinator(numel(a),2,'c','r')),2)
  댓글 수: 5
Stephen23
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
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개)

Andrei Bobrov
Andrei Bobrov 2016년 9월 23일
편집: Andrei Bobrov 2016년 9월 23일
b = a(:)*a(:).';
out = b(tril(true(size(b))));

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by