Matlab function help with returning minimum in an array
1 Comment
Accepted Answer
More Answers (3)
0 votes
17 Comments
- Which index: the row index, the column index, the linear index? A logical index? Or both subscripts at once? Which of these is the "proper" index that it should return? MATLAB has three supported indexing paradigms, all of which are useful in different situations. Which one do you want here?
- Which minimum/s: the minimum per row (as this code seems to do), or the minimum overall in the entire input array?
indmin(hcat(dPlus, dMinus))
"appears to be the matlab equivalent in Julia...?"
Nope: remember that your original MATLAB code returns the row index, not the linear index like your Julia code. So you will need to wrap it all in ind2sub to get the row index. Because ind2sub also requires the array size it would be easiest to define it beforehand:
A = hcat(dPlus, dMinus) idx = ind2sub(size(A),indmin(A))
and then get the first element of idx.
Hmmm... that reminds me of something someone told me once about MATLAB:
"In the original matlab code, dPlus and dMinus are concatenated aren't they? So concatenated they are the "A" matrix?"
Yes, that is correct: they are horizontally concatenated, using the [] shorthand operator. See:
https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
"The matlab code takes the minimum elements in each row and returns them in column form"
Correct. Then it returns the row index of the minimum in that column.
"My julia is essentially taking all the elements and putting them in column form and returning the index of the minimum element?"
Correct. Then it uses ind2sub to find the equivalent row index.
Note that MATLAB also has ind2sub, so exactly the same method could be applied in MATLAB. But Julia does not seem to natively implement anything equivalent to MATLAB's dimension argument...
I am finished for today. Thank you for the interesting question and discussion!
0 votes
1 Comment
0 votes
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!