How to code my own Reshape function without using the built in reshape()??

Im essentially trying to make a function ---> function [matrix] = myReshape(vector, m, n) that takes in a 1d vector and dimensions m and n, and makes it a 2d matrix as shown above with the m and n provided.
How can I go about this?

2 件のコメント

James Tursa
James Tursa 2020 年 10 月 16 日
編集済み: James Tursa 2020 年 10 月 16 日
Since MATLAB stores values in column order, note that your proposed "reshape" reorders the values in memory. I.e., it does not do the same thing as the MATLAB reshape( ) function. You have the equivalent of a transpose mixed in with this operation.
Coleman tyson
Coleman tyson 2020 年 10 月 16 日
I see. Though transpose wouldn't necessarily work for me because if I want to end up with an uneven matrix of dimensions 3x4, I couldn't reshape and then transpose the reshaped matrix, because it would result in a 4x3 matrix ://

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

 採用された回答

Matt J
Matt J 2020 年 10 月 16 日
編集済み: Matt J 2020 年 10 月 16 日
Hint: Notice the effect of doing things like this:
A=nan(3,4);
A(:)=1:12
A = 3×4
1 4 7 10 2 5 8 11 3 6 9 12

7 件のコメント

Coleman tyson
Coleman tyson 2020 年 10 月 16 日
Yeah that makes sense, but it gives the order of the values in the wrong orientation. If you look at the og question, I don't really want it to order downwards but rather horizontally. Does that make sense??
Matt J
Matt J 2020 年 10 月 16 日
Yep, you will have to figure out how to interchange rows and columns in some way.
Coleman tyson
Coleman tyson 2020 年 10 月 16 日
hmm Ok. Thanks for the tips.
Coleman tyson
Coleman tyson 2020 年 10 月 16 日
Wait so do you think I could do this, then transpose this array, then since the dimensions are also flipped, I could turn it into a 1d vector again, and then do this reshape thing again and then It would be ordered correctly? or is there a mistake with my logic here?
Matt J
Matt J 2020 年 10 月 16 日
Way more complicated than you need.
Coleman tyson
Coleman tyson 2020 年 10 月 16 日
lol ok sorry D: so do you recommend using transpose at all??
Matt J
Matt J 2020 年 10 月 16 日
編集済み: Matt J 2020 年 10 月 16 日
I recommend that you work backwards from the solution. What if you took the result that you are trying to get, and transposed that? What would that look like?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2020 年 10 月 16 日

編集済み:

2020 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by