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 commentsProblem Recent Solvers49
Suggested Problems
-
It dseon't mettar waht oedrr the lrettes in a wrod are.
1899 Solvers
-
792 Solvers
-
Matrix with different incremental runs
522 Solvers
-
748 Solvers
-
Create Volcano (or Atoll) martix. It is an extension of Bullseye matrix problem.
142 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!