Transpose in Matlab

I have the following code shown below.

I don’t get why you need to transpose the matrix.

Could someone explain why a transpose is needed here ?

Code:

function b = back_and_forth(n)
    b = reshape([1:1:n^2],[n,n])’
    b([2:2:n], : ) = b([2:2:n],[end:-1:1])
end 
2

1 Answer

Transposing and Complex Transposing Conventions

It is due to how reshape() shapes the vector. In this case reshape() reshapes the 1-by-16 vector into a 4-by-4 array by traversing column-wise. In this case, you must take the transpose .' or ' complex-conjugate transpose to make each column effectively be a row, similar to rotating the matrix. Visually:

Test Script:

n = 4;    
b = reshape([1:1:n^2],[n,n]);
b
b'
b = b';

b([2:2:n],:) = b([2:2:n],[end:-1:1]);
Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.