Problem 1896. Index one element in each vector of an array along a given dimension
Functions like min and max can return in their second output argument the index of the element in each vector along a particular dimension which meets the particular criterion being requested. E.g.
A = rand(3); [B, I] = min(A, [], 2);
Write a function which, given an array A, and an index array I (like the one created above), can reconstruct the first output of functions like min and max. E.g.
C = dimsel(A, I);
such that C is equal to B.
Solution Stats
Problem Comments
-
8 Comments
Good problem, but, in practice, no one would ever do that. It is better to store the global-linear index and do a quick calculation to find the needed position than to loop through the local indexes. The MATLAB function max has the option 'linear' because of that.
PS: I liked it anyway because it's a good conceptual problem for students to bear down.
Is the "given dimension" always 2?
Also, provide a few clean test cases. (Here's the input, here's the expected outputs). Or at least let me copy and paste the test case.
* The first [2,6] dimensions of A are non-singleton; A is singleton in all dimensions above first singleton.
* d, the "given dimension", is one of the non-singleton dimensions of A, and the first singleton dimension of I.
* Values in I will be in 1:length(A,d)).
% size(B) should == size(I).
* The problem is that I is the index along the collapsed dimension (d). All other dimensions should use ":".
Solution Comments
Show commentsGroup

Computational Geometry III
- 20 Problems
- 9 Finishers
- LASER Tracker - Mirror Distance and Angle
- Distance between two GPS Coordinates
- Spherical surface triangulation
- Do the line-segments intersect?
- Esoteric Trigonometry
- LASER Tracker - Mirror Distance and Angle
- Do the line-segments intersect?
- Spherical surface triangulation
- Parallel vectors
- Vector parallel to plane?
- distance to a straight line (2D) given any 2 distinct points on this straight line
- Slicing the cube
- Pairwise Euclidean Distance
- Wayfinding 1 - crossing
- Wayfinding 2 - traversing
- Wayfinding 3 - passed areas
- Avalaible area: wall construction
- rectangle in circle
- Distance between two GPS Coordinates
- Cut an orange
- Packing oranges - one layer
- Radiation Heat Transfer — View Factors (1)
- Radiation Heat Transfer — View Factors (2)
- Radiation Heat Transfer — View Factors (3)
- Esoteric Trigonometry
Problem Recent Solvers49
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!