Matlab function help with returning minimum in an array

5 ビュー (過去 30 日間)
Ryan Lockwood
Ryan Lockwood 2018 年 4 月 13 日
編集済み: Stephen23 2018 年 4 月 20 日
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
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
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
Ryan Lockwood 2018 年 4 月 19 日
So to confirm, in plain english, the code returns the minimum element in each row into column form, then returns the index (row) of the minimum element?
  17 件のコメント
Stephen23
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
Ryan Lockwood 2018 年 4 月 20 日
Fair enough. I wasn't sure if Matlab was able to auto convert from the array to a single floating point number, and Julia cannot. Totally stumped on this one. Thanks anyway. Though.

サインインしてコメントする。


Ryan Lockwood
Ryan Lockwood 2018 年 4 月 19 日
I am actually not sure if the input [dMinus, Dplus] is vector or a matrix. Does the same apply still? Thanks
  1 件のコメント
Stephen23
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.

サインインしてコメントする。


Ryan Lockwood
Ryan Lockwood 2018 年 4 月 19 日
Would you happen to know what the equivalent to this is in Julia code?

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by