Index and Match a Subset Index in Google Sheets - google-sheets

I find the MAX of a metric called Positive Gamma (column J), and a corresponding value called Strike (column A) using MATCH. This match is the Key Gamma Strike.
=INDEX($A$2:$A, MATCH(MAX($J$2:$J), $J$2:$J, 0), 0)
Now I need to find the max Positive Gamma among all strikes above the Key Gamma Strike.
For example, if the Key Gamma Strike is 380, and the strike range goes to 400, I need to find the strike between 381 and 400 with the highest Positive Gamma.
I find the row location for each strike above the Key Gamma Strike, placing it into column K, as follows:
=IFERROR( INDEX( IF( MATCH($A$2:$A$550, $A$2:$A$550, 1) > MATCH(MAX($J$2:$J$550), $J$2:$J, 0), Match($A$2:$A$550, $A$2:$A$550, 1), "") ,0, 0), )
I'm struggling with finding the highest positive gamma from this range, and the corresponding strike.
There may be a better way to get this, I'm just not sure how. Feedback appreciated.
Here's a file with data matching the above formulas: https://docs.google.com/spreadsheets/d/1VGGjd1f47yCz9mvDHqRFEFYWJTq0w-Y8Mf8iGqFgO28/edit?usp=sharing

Assuming that you have the Key Gamma Strike value on the cell K1, you could use the following array formula:
=ARRAYFORMULA(MAX(IF($A:$A>$K$1;$J:$J;"")))
You can find an example here.

Related

Arrayformula + SUM + INDIRECT

I am trying to get the spreadsheet to use the value of L2 as a reference to calculate the cash value at the moment:
From the formula:
=arrayformula(IF(I2:I<>"",$L$2+SUM(INDIRECT("G2:G"&I2:I)),""))
But the lines below always return the same result, in this case 100.
I wonder where I am going wrong in creating this formula
Another detail:
I have made a chart ready to treat this cash growth over time, if you decide to help by looking straight at the spreadsheet, by re-adjusting the correct formula, please also take a look if the chart is working correctly according to the data and the movement of values, always the last row of columns G and H at the beginning of the graph
This is the expected result in Bank Column:
Capture the initial bankroll L2 and from it add the sequence of results achieved in each investment in COLUMN G
Link to spreadsheet
try:
=ARRAYFORMULA(QUERY(L2+MMULT(ARRAY_CONSTRAIN(SPLIT(REPT("0×",
ROW(INDIRECT("A1:A"&COUNTA(D2:D)+1))-1)&
TEXTJOIN("×", 1, INDEX(SORT({INDIRECT("G2:G"&COUNTA(D2:D)+1),
ROW(INDIRECT("G2:G"&COUNTA(D2:D)+1))}, 2, 0),,1)), "×"), 999^99,
COUNTA(D2:D)+1)*1, ROW(INDIRECT("G1:G"&COUNTA(D2:D)+1))^0),
"offset 1", 0))

How to get the sum of a column up to a certain value?

I have a google sheet that I am using to try and calculate leveling and experience points. Column A has the level and Column B has the exp needed to reach the next level. i.e. To get to Level 3 you need 600 exp.
A B
1 200
2 400
3 600
...
99 19800
In column I2 I have an integer for an amount of exp (e.g. 2000), in column J2 I want to figure out what level someone would be at if they started from 0.
Put this in column J and ddrag down as required. Rounddown(I2,-2) rounds I2 down to the nearest 100. Index match finds a match in column B and returns the value in column A of the matched row.
=index(A2:A100,match(ROUNDDOWN(I2,-2),B2:B100,0))
Using a helper column (for example Z): put =sum(B$1:B1) in cell Z1 and drag down. This will compute the sums required for each level. In J2, use the formula
=vlookup(I2, {B:B, Z:Z}, 2) + 1
which looks up I2 in column B, and returns the nearest match that is less than or equal to the search key. It adds 1 to find the level that would be reached, because your table has this kind of an offset to you: the entry against level N is about achieving level N+1.
You may want to put 0 0 on top of the table, to correctly handle the amounts under 200. Or treat them with a separate if condition.
Using algebra
In your specific scenario, the point amount required for level N can be computed as
200*(1+2+3+...+N-1) = 200*(N-1)*N/2 = 100*(N-1/2)^2 - 25
So, given x amount of points, we can find N directly with algebra:
N = floor(sqrt((x+25)/100)+1/2)
which means that the formula
=floor(sqrt((I2 + 25) / 100) + 1/2)
will have the desired effect in cell J2, without the need for an extra column and vlookup.
However, the second approach only works for this specific point values.

An idea for a complex function that finds in a sorted list of integers a number not bigger than a given one

I have, say, in A1 a text containing a sorted (eventually reversed) list of integers separated by some not-digit-char - for example "10, 123, 230, 750, 1034, 2003, 10101"; in B1 I have an integer n; I need a formula not involving other cells that returns:
n if n belongs to the list in A1;
otherwise, if n is not bigger than the maximum value in A1, the value in A1 immediately bigger than n (e.g., for n = 567 the returned value must be 750);
otherwise, an error.
In my opinion, the only way to solve the problem concerns regexp substitution (that Google Sheet supports), but until now I can't find a reasonable way to proceed.
Someone has a (different) idea?
Please try:
=index(SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),
MATCH(B1,SORT(TRANSPOSE(SPLIT(A1,", ")),1,0),-1))
in this formula I used search_type = -1 for match function:
MATCH(search_key, range, search_type)
search_key - The value to search for. For example, 42, "Cats", or
I24.
range - The one-dimensional array to be searched. If a range with both height and width greater than 1 is used, MATCH will return #N/A!.
search_type - [ OPTIONAL - 1 by default ] - The manner in which to
search.
1, the default, causes MATCH to assume that the range is sorted in ascending order and return the largest value less than or equal to
search_key.
0 indicates exact match, and is required in situations where range is not sorted.
-1 causes MATCH to assume that the range is sorted in descending order and return the smallest value greater than or equal to search_key.
Simplify the case
Suppose you have a cell with text sorted in descending:
The formula would be:
=index(SPLIT(A1,", "),MATCH(B1,SPLIT(A1,", "),-1))
Please try:
=if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1))
Above won't work for numbers lower than the first, but if required could be expanded to:
=if(B1<1*left(A1,find(",",A1)-1),1*left(A1,find(",",A1)-1),if(isnumber(find(B1,A1)),B1,index(split(A1,","),match(B1,split(A1,","),1)+1)))

How to sum the maximum value of each column in Google Spreadsheets?

I have a Google Spreadsheet of numbers. How do I take the maximum value from each column, and summarize them using only one formula? (No temp cells, no scripts.)
1 2 1
0 1 3
0 2 0
For the table above the result should be 6 (1+2+3, the maximum value of each column). But I'd like a solution that works for much larger tables, too.
As a more general question, I'd like to find out how I could fold 2D ranges into 1D arrays using an arbitrary operator (like MAX and SUM in this case).
Assuming your data in range A2:D, to get the maximum of every row (array output) try
=query(transpose(query(if(row(A2:D)>=transpose(row(A2:D)),transpose( A2:D)),"select max(Col1),max(Col2),max(Col3),max(Col4) ",0)),"Select Col2", 0)
If you need to process a lot of columns, this may be better
=ArrayFormula(QUERY(TRANSPOSE(QUERY(TRANSPOSE( A2:D) , "Select "&"MAX(Col"&JOIN( ", MAX(Col",ROW(INDIRECT( "YY1:YY"&ROWS(A2:A)))&")"))), "Select Col2", 0))
To sum, just wrap SUM() around the above formulas.
MAX by columns in A1:C3
=INDEX(QUERY({A1:C3},"Select "&"MAX(Col"&JOIN(", MAX(Col",SEQUENCE(COLUMNS(A1:C3))&")"),0),2)
MAX by rows in A1:C3
=TRANSPOSE(INDEX(QUERY(TRANSPOSE(A1:C3),"Select "&"MAX(Col"&JOIN(", MAX(Col",SEQUENCE(ROWS(A1:C3))&")"),0),2))
Substitute MAX with MIN to get the minimums.

How to define a custom similarity measure

I need some help defining a custom similarity measure.
I have a dataset whose elements are defined by 4 attributes.
As an example, consider the following two items:
Element 1:
A1: "R1", "R3", "R4", "R7"
A2: "H1"
A3 "F1", "F2"
A4 "aaa" "bbb"
Element 2:
A1: "R1", "R2"
A2: "H1"
A3 "F1", "F2"
A4 "aaa" "bbb" "ccc" "ddd" "eee" "fff"
I have to implement a similarity measure which should satisfies the following conditions:
1 - If A2 value is the same, the two elements must belong to the same cluster
2 - If two elements have at least one common value on A4, the who elements must belong to the same cluster.
I need to use a sort of weighted Jaccard measure. Is it mathematically correct to define a similarity measure that sums the jaccard distance of each attribute and then to add a sort of high weigth if condition 1 and 2 are satisfied for A2 and A4?
If so, how can I transform the similarity matrix into a distance matrix?
(1) Distance = 1 - similarity. This is a common characteristic.
(2) Summing the distances of the attributes is valid, although you may wish to scale it back to the [0, 1] range.
(3) Putting a high weight is not correct for what you've described. If the A2 or A4 values show a match, simply set the distance to 0. The clustering is a requirement, not merely strong advice. Is there some other semantic to your distance function, that you didn't want to take this route?
FYI, the basics for being a topological metric's distance function, D are:
D(a, a) = 0
D(a,b) = D(b,a)
D(a,b) + D(b,c) >= D(a,c)

Resources