Finding the value and index of the first occurence of the largest element in an array
By : WB130
Date : March 29 2020, 07:55 AM
it helps some times I'm looking for a way to find the value and index of the first occurrence of the largest element in an array (in ruby). I have not been able to find a way to achieve this with fewer than 2 lines of code and without traversing the array more than I would like. , I'm more interested in performance code :
user system total real
Yours 0.484000 0.000000 0.484000 ( 0.485868)
Cary's 36.094000 0.000000 36.094000 ( 36.412679)
require 'benchmark'
example = (1..10**8).to_a
Benchmark.bm(7) do x
x.report("Yours") {
value = example.max
index = example.index(value)
}
x.report("Cary's") {
value, index = example.each_with_index.max_by { n,i [n,i] }
}
end
value, index = example[0], 0
i = 0
example.each do v
if v > value
value = v
index = i
end
i += 1
end
index = example.index(value = example.max)

Finding index of largest negative and smallest positive element in array
By : user605509
Date : March 29 2020, 07:55 AM
This might help you I have an array as follows: , You can try, for max of negative: code :
list(Arr).index(max(Arr[Arr<0]))
list(Arr).index(min(Arr[Arr>0]))

C program to output the largest element of array with the index of the largest element of array
By : rlelis
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Your approach to find the max element in an array is fine. But it alters the original array. If it okay with you, then go ahead with it, else if you would like to keep the array in tact as well as find the max element with the index, then check the code which I have attached below. If you intend to keep the methodology same, then just add a variable, which you should update as and when you find a higher number, as follows: code :
int max_index; //declare before
if (arr[0] < arr[i]) {
max_index = i;
arr[0] = arr[i];
}
#include<stdio.h>
int main()
{
int i, max_index;
float arr[5], max;
printf("Please enter five numbers:\n ");
for (i = 0; i < 5; ++i)
{
scanf("%f", &arr[i]);
}
max = arr[0];//start off assuming that the 1st element is the max
for (i = 0; i < 5; i++)//now compare it with the rest of the array, updataing the max all along
{
if (arr[i] > max) {
max = arr[i];
max_index = i;
}
}
printf("Largest element = %.2f at index %d", max, max_index);
return 0;
}

find the index of largest, 2nd largest or third largest in a row of multidimensional array pyhton
By : Etim Mkpanam
Date : March 29 2020, 07:55 AM
Any of those help Let say I have the following two numpy arrays: , Here's one that scales well for generic ndarrays  code :
def maxindex(a, b, fillna=1):
sidx = a.argsort(1)
m = np.isin(sidx,b)
idx = m.shape[1]  m[...,::1].argmax(1)  1
out = np.take_along_axis(sidx,idx[...,None],axis=1).squeeze()
return np.where(m.any(1), out, fillna)
In [83]: a
Out[83]:
array([[ 1, 4, 6, 2, 5],
[ 3, 2, 7, 12, 1],
[ 8, 5, 3, 1, 4],
[ 6, 10, 2, 4, 9]])
In [84]: b
Out[84]: array([0, 1, 4])
In [85]: maxindex(a, b) # all rows
Out[85]: array([4, 0, 0, 1])
In [86]: maxindex(a[1], b) # second row
Out[86]: array([0])
In [105]: a
Out[105]:
array([[[ 1, 4, 6, 2, 5],
[ 3, 2, 7, 12, 1],
[ 8, 5, 3, 1, 4],
[ 6, 10, 2, 4, 9]],
[[ 1, 4, 6, 2, 5],
[ 3, 2, 7, 12, 1],
[ 8, 5, 3, 1, 4],
[ 6, 10, 2, 4, 9]]])
In [106]: maxindex(a, b)
Out[106]:
array([[4, 0, 0, 1],
[4, 0, 0, 1]])

finding the index of the largest element in an array
By : Pierre Taquet
Date : March 29 2020, 07:55 AM
will be helpful for those in need I want to write a code that returns the index of the largest element in a given array. However, when I try compiling, I get a message that I can't seem to understand. Although I have done the things that the error message tells, it keeps telling me to fix them. Please help this novice programmer! , This type of initialization can't be done outside of a method :

