Combining the matrix' values

4 ビュー (過去 30 日間)
jan
jan 2015 年 3 月 26 日
回答済み: Roger Stafford 2015 年 3 月 26 日
is there any predefined function let us to combine the values in a matrix example: let A the original matrix
if true
A= 1 2
4 5
7 8
end
the result that i'm loking for is
if true
C= 1 1 1 1 2 2 2 2
4 4 5 5 4 4 5 5
7 8 7 8 7 8 7 8
end
  3 件のコメント
James Tursa
James Tursa 2015 年 3 月 26 日
So you want the columns of C to be all possible combinations of the elements of the rows of A?
dpb
dpb 2015 年 3 月 26 日
Appears he's dividing the two values into 1/2^n groupings where n=1:nRows. That is the first new row is 2 subsets, the 2nd is 4, etc., ...
That said, other than coding a loop and repmat no magic solution leaps out at me at the moment...

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2015 年 3 月 26 日
編集済み: James Tursa 2015 年 3 月 26 日
C = allcomb(A(1,:),A(:,2),A(:,3))'
You can find allcomb (by Jos) on the FEX:
Or more generally,
B = mat2cell(A,ones(1,size(A,1)),size(A,2));
C = allcomb(B{:})';

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2015 年 3 月 26 日
I think the following generalizes what you have requested for a matrix, A, of arbitrary size:
[m,n] = size(A);
[J,I] = meshgrid(0:n*2^(m-1)-1,m-1:-1:0);
C = A(m*(mod(floor(J./2.^I),n)+1)-I);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by