Transposing matrix using reshape

Hello,
I am a student taking a class to learn matlab. For a project, our instructor is requiring us to transpose a function using the reshape command. Because of the way matlab reads matrixes, column-dominant, this is proving very difficult.
In essence, I need to perform the following using only reshape:
A = A'
or
A = [1,2,3;4,5,6] must become A = [1,4;2,5;3,6]
Thanks for your help.

10 comentarios

the cyclist
the cyclist el 21 de Mzo. de 2012
Are you able to post the full problem statement from your instructor?
Adam
Adam el 21 de Mzo. de 2012
Unfortunately, no. It's a segment at the end of a long pdf that would make no sense without context. I've found two or three other ways of doing this, but none involve reshape.
Adam
Adam el 21 de Mzo. de 2012
is there some detail I can clear up for you?
the cyclist
the cyclist el 21 de Mzo. de 2012
No, I just thought that perhaps there was something subtle in the problem student that you overlooked.
Geoff
Geoff el 21 de Mzo. de 2012
Well, are you allowed to use ANY other functions like size(), repmat() and colon()? =) What exactly is the restriction?
Adam
Adam el 21 de Mzo. de 2012
size or basic data analysis functions like max or mean would be the only other allowed functions.
Adam
Adam el 21 de Mzo. de 2012
also sort() and sortrows(), but those don't seem like they'd be much help.
Geoff
Geoff el 21 de Mzo. de 2012
And no looping? She wants a one-liner?
Geoff
Geoff el 21 de Mzo. de 2012
Seems to me like your instructor is teaching you to hate MatLab.
Walter Roberson
Walter Roberson el 21 de Mzo. de 2012
Is array indexing allowed? Are multiplication and addition allowed?

Iniciar sesión para comentar.

Respuestas (5)

Walter Roberson
Walter Roberson el 21 de Mzo. de 2012

1 voto

reshape() by itself cannot be used to transpose a matrix unless the matrix happens to be a vector. If the matrix is not a vector then transpose alters the internal storage order of the elements, whereas reshape() never does.
For example, internally [1 2; 3 4] is stored in the order 1 3 2 4, and transpose of [1 2;3 4] would be [1 3;2 4] which would be stored in the order 1 2 3 4. You can see that the 2 and 3 have swapped internal places in the transpose. Reshape never swaps internal orderings.
James Tursa
James Tursa el 21 de Mzo. de 2012

1 voto

This is an ill-posed problem or something is missing from the problem statement. There are various ways to accomplish a transpose via indexing or permute etc as has already been pointed out. None of these involve the reshape function and as Walter points out reshape never alters the internal memory order (which is required for a general matrix transpose) so how the heck is reshape supposed to be involved in this in the first place?
Image Analyst
Image Analyst el 21 de Mzo. de 2012
Are you sure he didn't mean permute?
permute(A, [2 1])
ans =
1 4
2 5
3 6

1 comentario

Adam
Adam el 21 de Mzo. de 2012
I'm sorry, but no. She explicitly said reshape. Thanks for trying to help, though.

Iniciar sesión para comentar.

Geoff
Geoff el 21 de Mzo. de 2012
Okay, got a solution. Your matrix (let's just use the example of A) can be indexed by the vector 1:6, but you need to translate this index to be row-wise instead of column-wise.
I = 1:size(A(:),1)
So first, work out how to generate your row and column indices so that you end up with something like:
r = [1 1 1 2 2 2]
c = [1 2 3 1 2 3]
Then use those to generate a transposed index for A, which will end up like this:
It = [1 3 5 2 4 6]
After you have that, it should be obvious what to do.
Jan
Jan el 21 de Mzo. de 2012
As long as the problem is ill-posed, weird solutions are valid:
B = feval(reshape(['tno'; 'rss'; 'ape'], 1, 9), A);

1 comentario

Jan
Jan el 21 de Mzo. de 2012
The similarities between ...rssape and reshape are magic.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 21 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by