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 ?

enter image description 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]); 

Before Transposing → b:

After Complex-Conjugate Transposing → b':


Extension:

For complex numbers ' does more than transposing. It also takes the complex conjugate.

Code Snippet: Taking the complex-conjugate and transposing

Complex_Number = 5 + 2i; Complex_Number' 

Returns:

ans =

5.0000 - 2.0000i


Code Snippet: Taking the transpose

Complex_Number = 5 + 2i; Complex_Number.' 

Returns:

ans =

5.0000 + 2.0000i

Ran using MATLAB R2019b

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy