reshaping and transposing question

5 ビュー (過去 30 日間)
Savanna
Savanna 2015 年 2 月 22 日
編集済み: Alan Silver 2022 年 4 月 24 日
So my teacher gave us this problem where there's an array
C=[1 7; 2 8; 3 9; 4 10; 5 11; 6 12]
What he wants us to do with this array is use the reshape function with transpose in a single command to output a row vector which equals
[1 4 7 10 2 5 8 11 3 6 9 12].
I understand how the reshape and transpose work together, but i have no idea how to change the order of the numbers with the two functions. Any suggestions?
  2 件のコメント
Roger Stafford
Roger Stafford 2015 年 2 月 22 日
Be courageous! Try various successive reshapes and transposes. It takes only a few, namely three, to do the trick.
John D'Errico
John D'Errico 2015 年 2 月 22 日
Think about what order the elements are stored in memory. What do you know about this? Once you understand how MATLAB stores those elements, the reshape and transpose combination should be obvious.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2015 年 2 月 23 日
Yep. I got it done with one reshape, and two transposes. Two lines of code and one intermediate variable created, which was the result of the reshape. A brilliant engineer like Savanna should be able to get it done in a few minutes. Keep in mind that MATLAB goes down rows in a column first, before it moves over to the next column. Hint: Look up the colon : operator, and the reshape() function of course.

Alan Silver
Alan Silver 2022 年 4 月 24 日
編集済み: Alan Silver 2022 年 4 月 24 日
This one command composed of two reshapings and one tranpose will
do exactly what is shown.
D = reshape(transpose(reshape(C,3,4) ),1,12) ;
Innermost
reshape(C,3,4) gives
[ 1 4 7 10
2 5 8 11
3 6 9 12 ]
then , transposing it gives
[
1 2 3
4 5 6
7 8 9
10 11 12 ]
Then, reshaping again by 1 X 12
gives
[ 1 4 7 10 2 5 8 11 3 6 9 12 ]

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by