[Numpy-discussion] Indexing question

Neal Becker ndbecker2 at gmail.com
Thu May 20 10:05:14 EDT 2021


Thanks!

On Thu, May 20, 2021 at 9:53 AM Robert Kern <robert.kern at gmail.com> wrote:
>
> On Thu, May 20, 2021 at 9:47 AM Neal Becker <ndbecker2 at gmail.com> wrote:
>>
>> This seems like something that can be done with indexing, but I
>> haven't found the solution.
>>
>> out is a 2D array is initialized to zeros.  x is a 1D array whose
>> values correspond to the columns of out.  For each row in out, set
>> out[row,x[row]] = 1.  Here is working code:
>> def orthogonal_mod (x, nbits):
>>     out = np.zeros ((len(x), 1<<nbits), dtype=complex)
>>     for e in range (len (x)):
>>         out[e,x[e]] = 1
>>     return out
>>
>> Any idea to do this without an explicit python loop?
>
>
>
> i = np.arange(len(x))
> j = x[i]
> out[i, j] = 1
>
> --
> Robert Kern
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion



-- 
Those who don't understand recursion are doomed to repeat it


More information about the NumPy-Discussion mailing list