Skip to content

Indexed assignment with logical indices: subarray.jl error #45903

Closed
@sebastiantk

Description

@sebastiantk

Hi,

I have run into the following error:
I have a BitArray whose entries I want to change based on another array

A = BitArray(rand(0:1, 3,3))
pos = randn(3,3)
A[pos .< 0]  .= 0

This yields the error message:

ERROR: ArgumentError: number of indices (1) must match the parent dimensionality (2)
Stacktrace:
[1] check_parent_index_match(parent::BitMatrix, #unused#::Tuple{Bool})
@ Base ./subarray.jl:43
[2] check_parent_index_match(parent::BitMatrix, indices::Tuple{Vector{Int64}})
@ Base ./subarray.jl:41
[3] SubArray
@ ./subarray.jl:21 [inlined]
[4] SubArray
@ ./subarray.jl:32 [inlined]
[5] SubArray
@ ./subarray.jl:28 [inlined]
[6] materialize!(B::Base.Broadcast.BitMaskedBitArray{2, 2}, bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(identity), Tuple{Int64}})
@ Base.Broadcast ./broadcast.jl:1174
[7] top-level scope
@ REPL[10]:1

The indexing works without an error, the error only appears when I try to assign the new value.

Also, using Cartesian indices instead produces the expected result:

A = BitArray(rand(0:1, 3,3))
pos = randn(3,3)
A[findall(pos .< 0)]  .= 0

versioninfo()
Julia Version 1.7.2
Commit bf53498 (2022-02-06 15:21 UTC)
Platform Info:
OS: macOS (arm64-apple-darwin21.2.0)
CPU: Apple M1 Pro
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS =

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions