Clear Filters
Clear Filters

Semi-transpose a matrix

2 views (last 30 days)
Homayoon
Homayoon on 12 Jul 2016
Edited: Andrei Bobrov on 12 Jul 2016
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

Answers (2)

Andrei Bobrov
Andrei Bobrov on 12 Jul 2016
Edited: Andrei Bobrov on 12 Jul 2016
" 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 on 12 Jul 2016
Edited: Azzi Abdelmalek on 12 Jul 2016
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 Comments
Homayoon
Homayoon on 12 Jul 2016
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 on 12 Jul 2016
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))

Sign in to comment.

Categories

Find more on Wind Power in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!