Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:47:47+00:00 2026-06-05T23:47:47+00:00

The exercise in this tutorial says: Generate a 10 x 3 array of random

  • 0

The exercise in this tutorial says:

Generate a 10 x 3 array of random numbers (in range [0,1]). For each row, pick the number closest to 0.5.

Use abs and argsort to find the column j closest for each row.

Use fancy indexing to extract the numbers. (Hint: a[i,j] – the array i must contain the row numbers corresponding to stuff in j.)

So I did everything, but I feel the slicing method I used (and the initialisation of b) is not pythonic at all:

a = np.random.rand(10,3)

mask = np.argmin(abs(a-0.5), axis = 1)

b = np.ones(mask.size)

for j in range(0,mask.size):
    b[j] = a[j,mask[j]]

What is the other way of doing this without using the for loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:47:50+00:00Added an answer on June 5, 2026 at 11:47 pm
    import numpy as np
    a = np.random.rand(10,3)
    b = np.argmin(abs(a - .5), axis=1).choose(a.T)
    
    # a
    array([[ 0.97272372,  0.45351387,  0.19105835],
           [ 0.27895897,  0.12438789,  0.64857335],
           [ 0.05298066,  0.58122882,  0.805319  ],
           [ 0.39952727,  0.77728036,  0.65742471],
           [ 0.36522802,  0.06938552,  0.6595684 ],
           [ 0.9030323 ,  0.08965774,  0.01823633],
           [ 0.30996923,  0.53400339,  0.87600912],
           [ 0.17953532,  0.4888832 ,  0.0746074 ],
           [ 0.09052476,  0.47397504,  0.30317449],
           [ 0.31851577,  0.68135476,  0.38335483]])
    
    # b
    array([ 0.45351387,  0.64857335,  0.58122882,  0.39952727,  0.36522802,
            0.9030323 ,  0.53400339,  0.4888832 ,  0.47397504,  0.38335483])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In day 2 of the go tutorial there is this exercise: Why may it
This is an exercise from the standard ML tutorial seen here: http://homepages.inf.ed.ac.uk/stg/NOTES/node42.html I am
I'm stuck on a portion of the Java tutorial, specifically this exercise . The
Exercise: There's too much repetition in this file. Use strings, formats, and escapes to
I'm trying to solve the last exercise of this JavaScript Closure Tutorial which takes
I am a Django beginner, and I want to make this tutorial as exercise:
This is from Sun's tutorial's exercise area so I suppose it is homework. I
For this exercise I'm making I want a decimal < 4096 to be written
I am doing this small exercise. declare @No decimal(38,5); set @No=12345678910111213.14151; select @No*1000/1000,@No/1000*1000,@No; Results
This is a programming exercise from Chapter 6 of The Art and Science of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.