Matlab function help with returning minimum in an array
7 views (last 30 days)
Show older comments
Hello, I am tasked with translating matlab code into Julia. I know Julia better than I do Matlab, so I am trying to understand a line of code:
[~,pStar] = min(min([dPlus,dMinus],[],2))
pStar, dPlus, dMinus are variables that I have defined earlier. I am not entirely sure what the function is doing. I did read the function explanation on the mathworks site, but I still don't understand it entirely. Thank you.
1 Comment
Stephen23
on 13 Apr 2018
"pStar, dPlus, dMinus are variables that I have defined earlier"
The code you have shown allocates the second output of min to the variable pStar, so any previous definition of pStar is irrelevant and will simply be discarded.
Accepted Answer
Stephen23
on 13 Apr 2018
Edited: Stephen23
on 13 Apr 2018
[~,pStar] = min(min([dPlus,dMinus],[],2))
[~, % ignores this output
pStar] % allocates second output (index) to pStar
= min( ) % call min with one input argument
min( 2) % call min, third input = min along second dimension.
,[], % only to ensure position of the third input argument
[dPlus,dMinus] % concatenate dPlus and dMinus horizontally
If the input [dPlus,dMinus] is a matrix then the code will find the row with the minimum value in it. You can check this be trying a simple example:
>> M = [1,1,1;1,1,0;1,1,1]
M =
1 1 1
1 1 0
1 1 1
>> min(M,[],2)
ans =
1
0
1
>> [~,row] = min(min(M,[],2))
row = 2
More Answers (3)
Ryan Lockwood
on 19 Apr 2018
17 Comments
Stephen23
on 20 Apr 2018
Edited: Stephen23
on 20 Apr 2018
@Ryan Lockwood: I moved the code to a file, and uploaded this to your comment. If the MATLAB code runs correctly then this is a Julia problem, and you should ask on a Julia forum.
I guess you will have to do basic debugging: work through the variables, break that line down into its atomic operations, identify where the problem occurs, read the Julia documentation to see how to fix it, and compare against the MATLAB documentation. Do experiments and test what you are seeing on small sample data.
Ryan Lockwood
on 19 Apr 2018
1 Comment
Stephen23
on 19 Apr 2018
Edited: Stephen23
on 19 Apr 2018
The inner min has the dimension specified, so it will always operate along the second dimension of the input, regardless of the shape of the input. This means that for any input array of size AxBxCx... it will return an array of size Ax1xCx...
The outer min will operate along the first non-singleton dimension. If the input has no more than two non-singleton dimensions (of which one is the second) then the outer min will get a vector input, and its output will be one single value. Otherwise it will return an array. As in your question you did not write what size dPlus and dMinus have I cannot say more than this.
See Also
Categories
Find more on Logical 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!