Combining the matrix' values
이전 댓글 표시
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
2015년 3월 26일
You need to supply a LOT more detail about what the rules are for generating C from A. Otherwise we are left with too much guesswork. E.g., this works given what you have posted so far, but I am pretty sure it isn't what you want:
if( isequal(A,[1 2 3 4 7 8]) )
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
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
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...
채택된 답변
추가 답변 (1개)
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);
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!