arrays - Matlab: Return input value with the highest output from a custom function -
i have vector of numbers this:
myvec= [ 1 2 3 4 5 6 7 8 ...]
and have custom function takes input of 1 number, performs algorithm , returns number.
cust(1)= 55, cust(2)= 497, cust(3)= 14, etc.
i want able return number in first vector yielded highest outcome.
my current thought generate second vector, outcomevec
, contains output custom function, , find index of vector has max(outcomevec)
, match index myvec
. wondering, there more efficient way of doing this?
what described way it.
outcomevec = myfunc(myvec); [~,ndx] = max(outcomevec); myvec(ndx) % input produces max output
another option loop. saves little memory, may slower.
maxoutputvalue = -inf; maxoutputndx = nan; ndx = 1:length(myvec) output = myfunc(myvec(ndx)); if output > maxoutputvalue maxoutputvalue = output; maxoutputndx = ndx; end end myvec(maxoutputndx) % input produces max output
those pretty options.
you make fancy writing general purpose function takes in function handle , input array. method implement 1 of techniques above , return input value produces largest output.
Comments
Post a Comment