reshape question

68 views
Skip to first unread message

Gabor

unread,
Apr 27, 2013, 2:34:49 PM4/27/13
to julia...@googlegroups.com
I would like to view the same data sometimes as 1D and sometimes as 2D.
 
A simple test case is this:
 
> a = reshape(1:4,2,2)
> b = reshape(a,4)
> a[1] = 99

> a
2x2 Int32 Array:
 
99  3
 
2  4

> b
4-element Int32 Array:
 
99
 
2
 
3
 
4


Fine, it works as expected.
Let's try this the other way around:

> a = reshape(1:4,4)
> b = reshape(a,2,2)
> a[1] = 99

> a
2x2 Int32 Array:
 
99  3
 
2  4

> b
4-element Int32 Array:
 
99
 
2
 
3
 
4


It works again.
Now, let's get rid of the first reshape:

> a = [1:4]
> b = reshape(a,2,2)
> a[1] = 99

> a
4-element Int32 Array:
 
99
 
2
 
3
 
4

> b
2x2 Int32 Array:
 
1  3
 
2  4
 
 
Bummer, it does not work as I expected.
 
Could someone please explain why?

Viral Shah

unread,
Apr 28, 2013, 12:32:29 AM4/28/13
to julia...@googlegroups.com
See this discussion: https://github.com/JuliaLang/julia/issues/2279

Also, in this case, you would be better off with just creating a 2D array, and indexing it with either 1 or 2 indices. All arrays can be accessed with linear indexing. You do not need to use reshape here:

julia> a = [1 2; 3 4]
2x2 Int64 Array:
 1  2
 3  4

julia> a[1]
1

julia> a[3]
2

julia> a[2,2]
4

Gabor

unread,
Apr 28, 2013, 1:58:22 AM4/28/13
to julia...@googlegroups.com
 
Thank you for the explanation and the pointer, Viral!
In the meantime I realized that my third example also works,
this behaviour was changed at the version 0.1.2  (March 8)
and I was still using the version 0.1.0 (February 19).
 
Reply all
Reply to author
Forward
0 new messages