user2192778
user2192778

Reputation: 455

How to transpose a matrix without MATLAB's transpose tool

I have an image, which I have listed as a matrix. I want to take the transpose of that image and then display that image on the screen.

I would like to know how to do this the "hard" way, ie without using MATLAB's transpose function.

Upvotes: 0

Views: 15900

Answers (5)

Justin Bieber
Justin Bieber

Reputation: 1

You can use rot90 with flipud/fliplr for this.

Upvotes: -1

Emily
Emily

Reputation: 61

function [B] = trans(A)
[r c] = size(A);
B = zeros(c,r);
for i = 1:r
    for j = 1:c
        B(j,i) = A(i,j)
    end
end
end

Upvotes: 6

Shai
Shai

Reputation: 114816

Are you allowed to use flipud and fliplr ?
In this case you can represent the transposition as a sequence of flips (I'll leave it to you to figure out the exact sequence).

Upvotes: 0

Jonas
Jonas

Reputation: 74940

Here's hint toward a solution: The transpose transforms the element A(1,2) from the normal array A into the element B(2,1) from the transposed array B (B=A'), and vice versa. Thus, you can iterate through all the rows and column, and apply the transformation element-by-element.

Upvotes: 0

Glen O
Glen O

Reputation: 733

As this is for a class, I won't give you an exact answer, but I will nudge you in the right direction. I'm assuming that you are looking for a method that involves manually transposing the information, rather than using builtin functions.

Matlab stores values in a matrix in the form of a vector and a "size" - for instance, a 2x3 matrix would be stored with six values in a vector, and then [2,3] (internally) to tell it that it's 2x3 and not 6x1.

For the 2x3 matrix, this is the order of the values in the vector:

1 3 5
2 4 6

To reference the value in (2,2), you can reference it as A(2,2), or as A(4). The value in (1,3) can be referenced as A(5).

As such, if you can construct a vector referencing the values in the transposed order, then you can assign the new values into the appropriate order and store them in a matrix of appropriate size. To make the point, consider the transpose of the above matrix:

1 2
3 4
5 6

This would be represented as (1,3,5,2,4,6) with size (3,2). If you can construct the vector (1,3,5,2,4,6), then you can use that vector to assign the values appropriately.

Upvotes: 1

Related Questions