Column max for each row in matrix
Consider the matrix:
M = [1.0 2.0 3.0; 5.0 4.0 3.0; 1.0 100.0 12.0]
I want to get the column of the maximum value in each row. Therefore it should be:
col = [3; 1; 2]
as
M[1,3] -> 3.0;
M[2,1] -> 5.0;
M[3,2] -> 100.00;
It is easy to achieve in Octave:
[max, col] = max(M,[],2)
where col = [3; 2; 1].
In Julia, I can only find a function findmax
that returns the absolute index of the maximum element for each row. So it would be:
max, aindx = findmax(M,2)
where aindx = [7,2,6]
M [7] = 3.0; M [2] = 5.0; M [6] = 100;
Where can I find Julia equal for Octave max (M, [], 2)?
My current workaround:
max, aindx = findmax(M, 2); msize=size(M); col = zeros(msize[1], 1); for i=1:msize[1] _, col[i] = ind2sub(msize,aindx[i]); end
source to share
Julia is findmax
more flexible than Octave max
: you can find the maximum in several measurements at the same time. As a consequence, it returns a linear index.
As you noted, you can use ind2sub
to calculate the desired index (s). If you use this a lot, you can define your "workaround" as a function to make it easy to use. You can put this function in yours .juliarc.jl
if you want it to be always available.
source to share