Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following upon previous conversation, this PR follows on resolve #215 and an added solution for creating a centered subpart.
Question 80: Consider an arbitrary array, write a function that extracts a subpart with a fixed shape and centered on a given element (pad with a
fill
value when necessary)Previous Conversation for Context:
Me:
Current solution output by author:
Output centered subpart of the array, R:
As you can see above the output sub array array is clearly not centered and is also fixed, meaning no matter how many times you run you always take the same subpart of the array (indices wise, of course Z is randomly generated every time).
Updated solution (showing multi dimensional array):
Random array of shape (5,5,5):
[[[0.41318587, 0.02686425, 0.27634727, 0.34828896, 0.53552608],
[0.54426485, 0.40573159, 0.32785901, 0.95110226, 0.74314067],
[0.20954932, 0.28623217, 0.70648967, 0.8790684 , 0.58088525],
[0.93627437, 0.9338704 , 0.26930749, 0.28473462, 0.57721401],
[0.26036955, 0.2339317 , 0.41383287, 0.32453434, 0.5999981 ]],
[[0.17264282, 0.09969286, 0.54365084, 0.24587597, 0.63170746],
[0.73230861, 0.61695342, 0.33547145, 0.32364859, 0.30090559],
[0.82022138, 0.25265008, 0.20509734, 0.79525084, 0.69119536],
[0.37488242, 0.70970922, 0.49893688, 0.76578014, 0.82251079],
[0.02927624, 0.5578041 , 0.72246968, 0.23032316, 0.82340544]],
[[0.52939221, 0.35862534, 0.68652225, 0.17703581, 0.79556611],
[0.628605 , 0.49367779, 0.92209193, 0.30016312, 0.64213181],
[0.04117666, 0.2536342 , 0.70921765, 0.00874844, 0.50106527],
[0.47607017, 0.61145768, 0.85605809, 0.3551498 , 0.15807602],
[0.08091961, 0.02181234, 0.35937453, 0.2406722 , 0.05558978]],
[[0.62017971, 0.64246925, 0.28136912, 0.30680361, 0.80700583],
[0.01607795, 0.30556255, 0.84313362, 0.21627144, 0.81764976],
[0.12892675, 0.14246595, 0.59990127, 0.88475525, 0.4042635 ],
[0.07722488, 0.07539892, 0.65651728, 0.19927838, 0.73940527],
[0.89474121, 0.24049369, 0.97836756, 0.21814119, 0.09843175]],
[[0.31122096, 0.82433984, 0.3487815 , 0.34754057, 0.60063218],
[0.99627488, 0.21458165, 0.10521583, 0.31445038, 0.94451234],
[0.95148131, 0.60257491, 0.18610522, 0.87703271, 0.62835079],
[0.35963395, 0.69448514, 0.47953283, 0.64439803, 0.5633606 ],
[0.17343141, 0.69441931, 0.02642798, 0.92214243, 0.65579668]]]
Output - a random centered subpart of shape (2,3,2):
[[[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]
[[0. 0. 0. 0. ]
[0. 0.17264282 0.09969286 0. ]
[0. 0.73230861 0.61695342 0. ]
[0. 0.82022138 0.25265008 0. ]
[0. 0. 0. 0. ]]
[[0. 0. 0. 0. ]
[0. 0.52939221 0.35862534 0. ]
[0. 0.628605 0.49367779 0. ]
[0. 0.04117666 0.2536342 0. ]
[0. 0. 0. 0. ]]
[[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]] (padded with fill value 0)
The updated solution not only takes a random subpart of a given shape, but also centers it evenly across multiple dimensions.
Please correct me if I am wrong but this was my understanding of the given question.
Thank you.