Matlab function help with returning minimum in an array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ryan Lockwood
am 13 Apr. 2018
Bearbeitet: Stephen23
am 20 Apr. 2018
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 Kommentar
Stephen23
am 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.
Akzeptierte Antwort
Stephen23
am 13 Apr. 2018
Bearbeitet: Stephen23
am 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
Weitere Antworten (3)
Ryan Lockwood
am 19 Apr. 2018
17 Kommentare
Stephen23
am 20 Apr. 2018
Bearbeitet: Stephen23
am 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
am 19 Apr. 2018
1 Kommentar
Stephen23
am 19 Apr. 2018
Bearbeitet: Stephen23
am 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.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!