필터 지우기
필터 지우기

Semi-transpose a matrix

조회 수: 5 (최근 30일)
Homayoon
Homayoon 2016년 7월 12일
편집: Andrei Bobrov 2016년 7월 12일
Dear Matlab Experts,
I want to block-transpose a matrix i.e. a set of elements are forming a block of elements. My goal is to transpose the blocks such that within each block, the order of elements remains unchanged. I guess the below example helps to clarify my question:
a = [1 2 3 4 5 6
0 3 2 1 2 3
0 0 0 1 1 1
8 9 9 1 2 3]
Now,Assume that each row plays a role of a block thus the matrix a has 4 blocks with the size of 4*1 that is,
a = [block1; block2; block3; block4]
I need to transpose in the block-based order so the desired outcome has the following form:
[1 2 3 4 5 6 0 3 2 1 2 3 0 0 0 1 1 1 8 9 9 1 2 3]
Your helps will be appreciated. Thank you so much

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2016년 7월 12일
편집: Andrei Bobrov 2016년 7월 12일
" blocks are the matrix of 2*3"
[m,n] = size(a);
p = 2;
q = 3;
out = cell2mat(mat2cell(a,p*ones(m/p,1),q*ones(n/q,1))');
or
a = [1 2 3 4 5 6
0 3 2 1 2 3
0 0 0 1 1 1
8 9 9 1 2 3];
[m,n] = size(a);
p = 2;
q = 3;
t = reshape(a,p,m/p,q,n/q);
z = permute(t,[1 4 3 2]);
out = reshape(z,n/q*p,[])

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 12일
편집: Azzi Abdelmalek 2016년 7월 12일
a = [1 2 3 4 5 6
0 3 2 1 2 3
0 0 0 1 1 1
8 9 9 1 2 3]
b=a'
b=b(:)'
Or
b=reshape(a',1,[])
  댓글 수: 2
Homayoon
Homayoon 2016년 7월 12일
Thanks Azzi, first a minor modification is needed in your answer which changes it to
b=reshape(a',1,[])
however, the question is general and your answer is limited to the cases when each row is treated as a block. How can you extend the answer to the cases such as
[ block1, block2
block3, block4] %which for the example provided in the question blocks are the matrix of 2*3 i.e.
block1 = [1 2 3;0 3 2] similar for other blocks.
the desired outcome in this case will be:
[block1, block3
block2, block4]
Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 12일
You can use cell arrays
b1=randi(10,4)
b2=randi(10,4)
b3=randi(10,4)
b4=randi(10,4)
A={b1,b2;b3,b4}
idx=reshape(1:numel(A),size(A))'
out=cell2mat(A(idx))

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by