COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### reshape() row-wise in matlab similar to Fortran?

• Email
• Follow

```Given  A = [1, 2, 3, 4, 5, 6 ], I wanted to reshape it to
matrix of size 2 rows by 3 columns

[1 2 3
4 5 6]

Not to, the default, which is

[1 3 5
2 4 6]

The default reshape() is column-wise, both in Matlab and in Fortran.

Matlab help

"B = reshape(A,m,n) returns the m-by-n matrix B whose
elements are taken column-wise from A"

I.e. when doing

EDU>> B = reshape(A,[2,3])

one gets

B =
1     3     5
2     4     6

But I found a nice option in Fortran, I think this is part
of new Fortran 90, which allows one to control the order,
so in Fortan, I can do

B = RESHAPE( A, [2,3], order=[2,1])

and now B is

1.00000000       2.0000000       3.0000000
4.0000000       5.0000000       6.0000000

(Without using order= option above, the output is
the same as Matlab, i.e. default is column-wise).

I found this option useful, as sometimes I want
to control the reshaping order.

I could not find such an option in Matlab reshape,
unless I missed it?

I know I can manipulate Matlab result obtained from
reshape() to get it to the shape I wanted, but I think
an option such as Fortran's order would be nice
to have.

ps. below is the Fortran code:

-------------
program test_reshape

implicit none
REAL, DIMENSION (6)   :: A = [1, 2, 3, 4, 5, 6]
REAL, DIMENSION (2,3) :: B
integer ::i,j

B = reshape( A, [2,3] )
write(*,*) 'default ordering'
CALL WRITE(B)

B = reshape( A, [2,3], order=[2,1])
write(*,*) 'row-wise ordering'
CALL WRITE(B)

!--- internal functions below ------
contains
SUBROUTINE write(A)
implicit none
REAL, DIMENSION(:,:) :: A
integer :: i,j

WRITE(*,*)
DO i = lbound(A,1), ubound(A,1)
WRITE(*,*) (A(i,j), j = lbound(A,2), ubound(A,2))
END DO

END SUBROUTINE write

end program  test_reshape
-----------------------------
\$ gfortran -std=f2003 t.f90

\$ ./a.exe
default ordering

1.00000000       3.0000000       5.0000000
2.0000000       4.0000000       6.0000000
row-wise ordering

1.00000000       2.0000000       3.0000000
4.0000000       5.0000000       6.0000000

--Nasser
```
 0

See related articles to this posting

```On Dec 5, 3:01=A0pm, "Nasser M. Abbasi" <n...@12000.org> wrote:

> I could not find such an option in Matlab reshape,
> unless I missed it?
>
> I know I can manipulate Matlab result obtained from
> reshape() to get it to the shape I wanted, but I think
> an option such as Fortran's order would be nice
> to have.

Why are you posting this here?  c.l.f is not the
Mathworks' support forum.  Nor is it the place to
ask the Mathworks for a new feature.

--
steve
```
 0

```On 12/5/2010 3:32 PM, steve wrote:
> On Dec 5, 3:01 pm, "Nasser M. Abbasi"<n...@12000.org>  wrote:
>
>> I could not find such an option in Matlab reshape,
>> unless I missed it?
>>
>> I know I can manipulate Matlab result obtained from
>> reshape() to get it to the shape I wanted, but I think
>> an option such as Fortran's order would be nice
>> to have.
>

> Why are you posting this here?  c.l.f is not the
> Mathworks' support forum.  Nor is it the place to
> ask the Mathworks for a new feature.
>
> --
> steve

The posting is related to Fortran, and might have some
intrinsic value to some who use Fortran as I posted
Fortran code to show how to reshape an array in different
ordering comparing it to Matlab.

I find it strange, that you will take time to actually
make such reply. There seem to be a different reason my
post ticked you off. I know that fortran group has the
most unfriendly people I've seen on any other newsgroup,
and you seem to top that.

With all the junk being written on the net each second,
that you will actually complain why I am posting small note
with some fortran code to c.l.f.

In addition, may I remind you, that you do not own the internet
newsgroup, and you do not own fortran news group, and you
are not the king who decides what kind can be posted here.

If you do not like to read my post, you have the freedom to
do so. And others have the freedom to post.

I suggest that you put me on your spam filter, please do,
so you do not see my posts. It is very simple to do. Just
google "spam filter" and you'll find one.

--Nasser
```
 0

```A little tricky, perhaps, but why not just transpose the result:

A = [1, 2, 3, 4, 5, 6 ]
B = reshape(A, [3 2])'

A =
1     2     3     4     5     6
B =
1     2     3
4     5     6
```
 0

```In comp.lang.fortran Nasser M. Abbasi <nma@12000.org> wrote:
> On 12/5/2010 3:32 PM, steve wrote:
>> On Dec 5, 3:01 pm, "Nasser M. Abbasi"<n...@12000.org>  wrote:

>>> I could not find such an option in Matlab reshape,
>>> unless I missed it?
(snip)

>> Why are you posting this here?  c.l.f is not the
>> Mathworks' support forum.  Nor is it the place to
>> ask the Mathworks for a new feature.
(snip)

> The posting is related to Fortran, and might have some
> intrinsic value to some who use Fortran as I posted
> Fortran code to show how to reshape an array in different
> ordering comparing it to Matlab.

It seems that both Fortran and Matlab keep them internally
in column major order.  For Matlab, and the default for Fortran,
the storage order is the same.

> I find it strange, that you will take time to actually
> make such reply. There seem to be a different reason my
> post ticked you off. I know that fortran group has the
> most unfriendly people I've seen on any other newsgroup,
> and you seem to top that.

It seems to me that you make the assumption that he was
ticked off, and being unfriendly.  That is possible, but do
note that you didn't really ask a question.  It might be
that he wasn't ticked off, but wanted to know if you did
have a question, possibly related to Fortran, that wasn't
so obvious.

> With all the junk being written on the net each second,
> that you will actually complain why I am posting small note
> with some fortran code to c.l.f.

c.l.f is pretty good about posts not so related to Fortran.
Now, you could have asked why Fortran did have that feature.
(I don't know, so it might make for interesting discussion.)

If you want to know why Matlab doesn't, then other places
are likely to get better answers.

> In addition, may I remind you, that you do not own the internet
> newsgroup, and you do not own fortran news group, and you
> are not the king who decides what kind can be posted here.

Note that he didn't say that you shouldn't post here, and didn't
claim to own the newsgroup.  He just asked a simple question.

> If you do not like to read my post, you have the freedom to
> do so. And others have the freedom to post.

Yes.

> I suggest that you put me on your spam filter, please do,
> so you do not see my posts. It is very simple to do. Just
> google "spam filter" and you'll find one.

This feature has been discussed before, though maybe not in
reference to Matlab.

-- glen
```
 0

```On 12/5/2010 6:48 PM, glen herrmannsfeldt wrote:

>
>> I find it strange, that you will take time to actually
>> make such reply. There seem to be a different reason my
>> post ticked you off. I know that fortran group has the
>> most unfriendly people I've seen on any other newsgroup,
>> and you seem to top that.
>

> It seems to me that you make the assumption that he was
> ticked off, and being unfriendly.  That is possible, but do
> note that you didn't really ask a question.  It might be
> that he wasn't ticked off, but wanted to know if you did
> have a question, possibly related to Fortran, that wasn't
> so obvious.
>

I know the question was on matlab not having an option
similar to Fortran reshape(). At the end added c.l.f as
a FYI type thing, since I thought some might find some
use for it.

If someone posted a FYI note to a newsgroup, which does
not a specific question in it, but may be some information,
the poster not need not be attacked for it or questioned
about why they are posting it.

Who said that one can only post "questions" to these newsgroup?
They are called "discussion" groups for a reason.

>> With all the junk being written on the net each second,
>> that you will actually complain why I am posting small note
>> with some fortran code to c.l.f.
>

> c.l.f is pretty good about posts not so related to Fortran.
> Now, you could have asked why Fortran did have that feature.
> (I don't know, so it might make for interesting discussion.)
>
> If you want to know why Matlab doesn't, then other places
> are likely to get better answers.
>

Again, I added c.l.f. becuase I had fortran code in it, and
thought it might be useful to some on clf to see the comparison
and how Fortran has this option for reshape() which matlab
does not.

>> In addition, may I remind you, that you do not own the internet
>> newsgroup, and you do not own fortran news group, and you
>> are not the king who decides what kind can be posted here.
>

> Note that he didn't say that you shouldn't post here, and didn't
> claim to own the newsgroup.  He just asked a simple question.
>

Yes, sure. He was just asking a "simple" question. And I

--Nasser
```
 0

```In comp.lang.fortran Nasser M. Abbasi <nma@12000.org> wrote:
(snip, I wrote)

>> It seems to me that you make the assumption that he was
>> ticked off, and being unfriendly.  That is possible, but do
>> note that you didn't really ask a question.  It might be
>> that he wasn't ticked off, but wanted to know if you did
>> have a question, possibly related to Fortran, that wasn't
>> so obvious.

> I know the question was on matlab not having an option
> similar to Fortran reshape(). At the end added c.l.f as
> a FYI type thing, since I thought some might find some
> use for it.

My newsreader doesn't indicate crossposts unless I follow-up
the post, and even then I don't always notice.  I often read
posts assuming (sometimes incorrectly) that it was posted
first to the group I am reading.  It would have helped
(though it isn't required) to note in the post that c.l.f.
was added as a second thought.

> If someone posted a FYI note to a newsgroup, which does
> not a specific question in it, but may be some information,
> the poster not need not be attacked for it or questioned
> about why they are posting it.

> Who said that one can only post "questions" to these newsgroup?
> They are called "discussion" groups for a reason.

(snip)

> Again, I added c.l.f. becuase I had fortran code in it, and
> thought it might be useful to some on clf to see the comparison
> and how Fortran has this option for reshape() which matlab
> does not.

That wasn't obvious to me, and maybe not to Steve, either.

-- glen
```
 0

```On Dec 5, 5:44=A0pm, "Nasser M. Abbasi" <n...@12000.org> wrote:
> On 12/5/2010 3:32 PM, steve wrote:
>
>
>
> > On Dec 5, 3:01 pm, "Nasser M. Abbasi"<n...@12000.org> =A0wrote:
>
> >> I could not find such an option in Matlab reshape,
> >> unless I missed it?
>
> >> I know I can manipulate Matlab result obtained from
> >> reshape() to get it to the shape I wanted, but I think
> >> an option such as Fortran's order would be nice
> >> to have.
>
> > Why are you posting this here? =A0c.l.f is not the
> > Mathworks' support forum. =A0Nor is it the place to
> > ask the Mathworks for a new feature.
>
> > --
> > steve
>
> The posting is related to Fortran, and might have some
> intrinsic value to some who use Fortran as I posted
> Fortran code to show how to reshape an array in different
> ordering comparing it to Matlab.
>
> I find it strange, that you will take time to actually
> make such reply. There seem to be a different reason my
> post ticked you off. I know that fortran group has the
> most unfriendly people I've seen on any other newsgroup,
> and you seem to top that.

I'm not ticked off.  I'm interesting in knowing why
you think posting about a deficiency in Matlab is
relevant in c.l.f?  I would ask the same question of
someone who posts a short program in Fortran in c.l.f,
and then asks why python, c, c++, or language-du-jour
does not support the feature.  The correct forum is one
that deals with that language.  Problems with Matlab
are off-topic for c.l.f.

--
steve
```
 0

```steve <kargls@comcast.net> wrote in message <299c62f7-c162-4416-8b8f-5ad4c5ef6473@u25g2000pra.googlegroups.com>...
>
>  I'm interesting in knowing why
> you think posting about a deficiency in Matlab is
> relevant in c.l.f?
> steve

My turn to ask the question : why a post about Matlab deficiency is not revelant in Matlab newsgroup? Any topic related to Matlab IS relevant. Afterall you are here at *Matlab* newsgroup.

Back to the question, what wrong with reshape(A,[m n]).'

Bruno
```
 0

```On 12/5/2010 10:44 PM, steve wrote:

>
> I'm not ticked off.  I'm interesting in knowing why
> you think posting about a deficiency in Matlab is
> relevant in c.l.f?

I really do not owe you an explanation. I already said
why I posted it.

> I would ask the same question of
> someone who posts a short program in Fortran in c.l.f,
> and then asks why python, c, c++, or language-du-jour
> does not support the feature.

Why would you even ask? Who appointed you the king of the
internet?

If you do not like a post, do not read it? Why are carrying
a stick on your shoulder and want to police the internet for?

>  The correct forum is one
> that deals with that language.  Problems with Matlab
> are off-topic for c.l.f.
>

It was related to Fortran. I explained that. But again, you
have the freedom not to read it. And I support your freedom
to do that.

--Nasser
```
 0

```Nasser M. Abbasi <nma@12000.org> wrote:

> Why would you even ask? Who appointed you the king of the
> internet?

the title comes from to judge that he shouldn't ask.

But I won't ask that as I don't believe the thread was declared
recursive. :-)

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain
```
 0

```["Followup-To:" header set to comp.lang.fortran.]

In <1jt1z43.stfqcd1qgzvyqN%nospam@see.signature> Richard Maine:

[Snip...]

> I don't believe the thread was declared recursive. :-)

LOL!!! You and Glen clearly outclass trolls in droll humor lately.  :)

--
Regards, Weird (Harold Stevens) * IMPORTANT EMAIL INFO FOLLOWS *
Pardon any bogus email addresses (wookie) in place for spambots.
Really, it's (wyrd) at airmail, dotted with net. DO NOT SPAM IT.

```
 0

```"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <idi2fs\$kpa\$1@fred.mathworks.com>...
> >
> >  I'm interesting in knowing why
> > you think posting about a deficiency in Matlab is
> > relevant in c.l.f?
> > steve
>
> My turn to ask the question : why a post about Matlab deficiency is not revelant in Matlab newsgroup? Any topic related to Matlab IS relevant. Afterall you are here at *Matlab* newsgroup.
>
> Back to the question, what wrong with reshape(A,[m n]).'
>
> Bruno

Doesn't the transpose consume some resources?
And what if I wanna reshape following [3,2,1] dimensions?  Then I need to permute the matrix...what if it was possible to choose at the beginning the directions along which to reshape.

Those are just thoughts anyways.

Oleg
```
 0

```On Dec 6, 8:21=A0am, "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it>
wrote:
>
> Doesn't the transpose consume some resources?
> Oleg
--------------------------------------------------
Who cares?  Exactly how big is your matrix anyway?  Is it tens or
hundreds of millions of elements?  If not, I wouldn't worry about it.

```
 0

```"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message
news:idio00\$2ah\$1@fred.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <idi2fs\$kpa\$1@fred.mathworks.com>...
>> steve <kargls@comcast.net> wrote in message
>> >
>> >  I'm interesting in knowing why
>> > you think posting about a deficiency in Matlab is
>> > relevant in c.l.f? steve
>>
>> My turn to ask the question : why a post about Matlab deficiency is not
>> revelant in Matlab newsgroup? Any topic related to Matlab IS relevant.
>> Afterall you are here at *Matlab* newsgroup.
>>
>> Back to the question, what wrong with reshape(A,[m n]).'
>>
>> Bruno
>
> Doesn't the transpose consume some resources? And what if I wanna reshape
> following [3,2,1] dimensions?  Then I need to permute the matrix...what if
> it was possible to choose at the beginning the directions along which to
> reshape.
>
> Those are just thoughts anyways.
>
> Oleg

> Doesn't the transpose consume some resources?
For a vector in MATLAB, .' (with no conjugation) basically costs essentially
nothing, but there is a cost for arrays.  For real data, ' and .' are
identical.
--
Loren
http://blogs.mathworks.com/loren/

```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <24b3c61c-a5a8-40cb-9f18-189ecd7ca337@a28g2000prb.googlegroups.com>...
> On Dec 6, 8:21 am, "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it>
> wrote:
> >
> > Doesn't the transpose consume some resources?
> > Oleg
> --------------------------------------------------
> Who cares?  Exactly how big is your matrix anyway?  Is it tens or
> hundreds of millions of elements?  If not, I wouldn't worry about it.

I had to manage more than 100 mln by 28 matrixes. Not that I was worried anyways since time and ram were enough to solve the problem.

Oleg
```
 0

```"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <idio00\$2ah\$1@fred.mathworks.com>...

>
> Doesn't the transpose consume some resources?

I imagine Fortran also does. How could it be differently when the elements in the memory are mixing up?

Bruno
```
 0

```
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:idi2fs\$kpa\$1@fred.mathworks.com...
> steve <kargls@comcast.net> wrote in message
>>
>>  I'm interesting in knowing why
>> you think posting about a deficiency in Matlab is
>> relevant in c.l.f? steve
>
> My turn to ask the question : why a post about Matlab deficiency is not
> revelant in Matlab newsgroup? Any topic related to Matlab IS relevant.
> Afterall you are here at *Matlab* newsgroup.

Nasser posted this both to comp.soft-sys.matlab (CSSM) and to
comp.lang.fortran (c.l.f in steve's post.)  steve objected to a MATLAB
question being posted to a Fortran newsgroup.

As a response to Nasser:  if you want to request this as an enhancement to
the RESHAPE function, please send it to Technical Support along with your
motivation (a mention of the Fortran functionality.)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
http://www.mathworks.com

```
 0

```"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <idisi9\$7l\$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <idio00\$2ah\$1@fred.mathworks.com>...
>
> >
> > Doesn't the transpose consume some resources?
>
> I imagine Fortran also does. How could it be differently when the elements in the memory are mixing up?
>
> Bruno

The MATLAB - Fortran reshape comparison is as follows:

MATLAB:
------------
The reshape function produces a shared data copy. i.e., the data stays in memory in the original element order ... it is only the shape that is new. The original array and the reshaped array physically point to the same data memory.

If you want to change the physical element order in memory you will need to use another function to do it, e.g. transpose or permute.  The transpose and permute functions will typically cause a data copy to occur (but there are some exceptions, e.g. transposing a vector).

If you want a new shape that has a different number of elements you have to do the padding/clipping manually prior to calling reshape. This will also cause a data copy.

Fortran:
----------
The reshape function always produces a data copy.  It has built-in support for permuting and padding so you can do it all in one function call. But because it always causes a data copy it is may not be useful for large arrays in some cases.

If you want a MATLAB-like shared data copy capability, you have to do extra work to get this behavior. Essentially you would use a Fortran pointer to point to the original array (does not cause a data copy). Then convert this Fortran pointer (which contains the original shape etc.) to another Fortran pointer with the desired new shape. This is necessary since Fortran pointers carry with them the shape, extents, and stride of the dimensions of the target. You couldn't do any padding or permuting with this scheme, of course, since that would necessitate a data copy.

Would it be worthwhile to have the padding & permuting capability built-in to the MATLAB reshape function?  Maybe. I have thought about this before, & even submitted a limited padding version of reshape to the FEX, but it hasn't been enough for me to formally request TMW to put in a new feature. On the other hand, I see no harm in making the request.

James Tursa
```
 0

```"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <idj70i\$ab8\$1@fred.mathworks.com>...
>
> MATLAB:
> ------------
> The reshape function produces a shared data copy. i.e., the data stays in memory in the original element order ... it is only the shape that is new. The original array and the reshaped array physically point to the same data memory.

I should qualify that statement and say that this behavior applies to m-files. If you call the reshape function from within a mex routine with mexCallMATLAB then a data copy will take place. My impression is (haven't verified this) that all function calls using mexCallMATLAB cause data copy results for functions that would normally return a shared data copy at the MATLAB m-file level. Probably as a precaution for the mex programmer, but it does make things more inefficient in the mex routine. For the reshape function in particular, one would be forced to use undocumented functions such as mxCreateSharedDataCopy and then manually set the dimensions of the result to get the same efficiency in a mex routine that MATLAB has at the m-file level.

James Tursa
```
 0