Skip to content

array assignment a[1,:,:] = b fails even when b is the correct size #4048

Closed
@ggggggggg

Description

@ggggggggg

Julia Studio 0.3.2

a = zeros((9,9,9))
b = ones((9,9))
println(size(a[1,:,:])) # (1,9,9)
a[1,:] = b
a[1,:,:] = b # argument dimensions must match

For comparison this works in both MATLAB and numpy.

python + numpy

In [10]: a = numpy.zeros((9,9,9))
In [11]: b = numpy.ones((9,9))
In [12]: a[1,:,:] = b
In [13]: a[1,:,:].shape
Out[13]: (9, 9)
In [14]: a[1,:] = b

Personally I feel it makes more sense to have the resulting array from a[1,:,:] have size (9,9) rather than (1,9,9). I also know that in MATLAB the leading singleton dimensions make plot fail where it would work with a 2d array, which is often annoying.

MATLAB

>> a = zeros(9,9,9);
>> b = ones(9,9);
>> a(1,:,:) = b;
>> size(a(1,:,:))
ans =
     1     9     9
>> a(1,:) = b;
Subscripted assignment dimension mismatch.

Here is a more extreme case, where I can't find any solution that works.

a = zeros((4,4,4,4))
b = ones((4,4))
a[1,1,:,:]=b # argument dimensions must match
a[1,1,:] = b # argument dimensions must match
a[1,:,1] = b # argument dimensions must match
a[1,:,:,1] = b # argument dimensions must match

All of the equivalent "extreme case" assignments do something in numpy. In MATLAB both of the unambiguous assignments (a[1,1,:,:]=b and a[1,:,:,1] = b) work, and the others do not.

I'm new to julia, so I'm not sure if you want the more ambiguous of these to work. For example its ambiguous in the "extreme case" what a[1,:,1] = b even means. But certainly the unambiguous subset of these assignments should work.

Also I don't know about the implementation, but from a user point of view this is similar to issue #4033

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingThis change will break codeneeds decisionA decision on this change is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions