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

조회 수: 11 (최근 30일)
Coleman tyson
Coleman tyson 2020년 10월 16일
편집: Matt J 2020년 10월 16일
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일
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개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by