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
The test case is buggy: the beginning of the for loop is on the same line as the %%. Problem is not solvable that way...
Hmm, ok the test works. However, there's no output at all, neither from inside the function nor from the passed/failed test cases. Very annoying...
Every test case should begin with "%%". If it doesn't, then it is treated in Cody as a Shared Variables section (variables available to every test case: intentionally utilized in Problem 44655).
Eventually I have found that this causes problems when the "%%" is unintentionally omitted from test cases, because it causes a very confusing suppression of test case output if an assertion in the nominal SV section fails, but suppresses the nominal Shared Variables section's output if all the SV section is passed without error!! Unfortunately there doesn't appear to be any official public documentation of this, and there's no Cody forum to post about it, but I have left a comment trail at Problems 520, 753 & 970. —DIV
I forgot to mention: if the author of the problem omits "%%" at the start of a Test Suite, Cody displays the Test Suite to the public as if "%%" were inserted before the actual start of the Test Suite. It's a little bit like the way consecutive spaces are hidden in default HTML rendering [or the opposite of that, because Cody is displaying characters that did not exist in the original!].
The "%%" problem here should be fixed now.
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 Solvers47
Suggested Problems
-
6796 Solvers
-
Find the peak 3n+1 sequence value
2492 Solvers
-
4374 Solvers
-
260 Solvers
-
1487 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!