Matlab function help with returning minimum in an array
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2018-4-13
"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.
采纳的回答
Stephen23
2018-4-13
编辑:Stephen23
2018-4-13
[~,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
更多回答(3 个)
Ryan Lockwood
2018-4-19
17 个评论
Stephen23
2018-4-20
编辑:Stephen23
2018-4-20
@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
2018-4-19
1 个评论
Stephen23
2018-4-19
编辑:Stephen23
2018-4-19
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!