Google Sheets - Increment cell value with ARRAYFORMULA and based on value from another field - arrays

I have a spreadsheet I'm creating and I have an ARRAYFORMULA for incrementing the number of a field based on another field. My formula looks like this (NOTE: my rows start on row 4 that is why there is a ROW(A4:A)-3):
=ARRAYFORMULA(IF(ROW(A4:A) = 4, 1, IF(B4:B = 1, ROW(A4:A)-3, (ROW(A4:A)-3)-(B4:B - 1))))
What I'm doing is creating groups (A) and then have a sequence counter (B) which is the number of rows within the group. I want the result to look like this where the A just picks up from where it left off (Note: B is manually entered):
A
B
1
1
2
1
3
1
3
2
3
3
4
1
5
1
However, my result is looking this this:
A
B
1
1
2
1
3
1
3
2
3
3
6
1
7
1
I know ROW gets me the row number but when I try and use INDEX formula:
=ARRAYFORMULA(IF(ROW(G4:G) = 4, 1, IF(H4:H = 1, INDEX(G4:G, ROW(G4:G)-1, 1) + 1, INDEX(G4:G, ROW(G4:G)-1, 1)))
to get the actual value of the prior cell I get a constant flashing like it's stuck in an infinite loop of some sort. I know I can probably just accomplish this without ARRAYFORMULA however, this spreadsheet will be shared and contains many other formulas that I just don't want people to have to cut and copy from the row above and get formulas all messed up. I'm dealing with non-technical people that need something to just work very simple.
Sample:
https://docs.google.com/spreadsheets/d/1FqndR4oTm_uaO7aUxYSgb3p0bZ6yBz-KnjUAD8kctd4/edit?usp=drivesdk

use:
=ARRAYFORMULA(COUNTIFS(A4:A, A4:A, ROW(A4:A), "<="&ROW(A4:A)))
reverse:
=ARRAYFORMULA(MMULT(TRANSPOSE((SEQUENCE(COUNTA(B4:B))<=
SEQUENCE(1, COUNTA(B4:B)))*IF(INDIRECT("B4:B"&COUNTA(B4:B)+ROW(B4)-1)=1, 1)),
SEQUENCE(COUNTA(B4:B))^0))

Related

Replace the values in a vector with the keys from a map in MATLAB?

I have a vector of nondecreasing data. Here is a sample:
1
1
1
2
2
2
2
2
2
2
2
3
3
4
4
6
Clearly there are duplicates and missing numbers. I can remove the duplicates using unique, so my unique values are:
uniqueVals = unique(sortedData);
So far, so good. Now, I want to change the data so that the values in sortedData are replaced with their index number in uniqueVals. For instance, uniqueVals first 5 elements would be 1,2,3,4,6, with indices 1,2,3,4,5. I want to change sortedData so that 1 maps to 1, 2 maps to 2, 3 to 3, 4 to 4, 6 to 5 and so on.
I know I can create a "map" object, but that seems to just be used to map uniqueVals to its index. How do I apply that mapping so that the entries in sortedData are changed?
I have no need for this to be a particularly fast operation. sortedData contains only a few hundred thousand rows and it only needs to be done once.
You can use the third output from unique
[uniqueVals,~,yourOutput] = unique(sortedData);
yourOutput =
1
1
1
2
2
2
2
2
2
2
2
3
3
4
4
5
You can also use g = findgroups(sortedData);, which will give you the group index, where there is one group per unique value. The 2nd output of this tells you the value itself
[g, gValue] = findgroups( sortedData );

Extracting data with Vlookup and checking values with Arrayformula

I need to extract data from one tab (extracted data) to another tab and validate the data in the following way:
if 0% assign 3
if from 0 till -10% assign 2
if from -10% and more assign 1
if from 0% till 10% assign 4
if from 10% and more assign 5
here is the link to the file https://docs.google.com/spreadsheets/d/1f8SFi2hNP6Anav7G7BYWyK-fasPk1pT1A2HFJblT-FI/edit?usp=sharing
I suggest you use two vlookups.
If you have a tab called 'Ranges' with the following two columns:
Percentage Result
-1000% 1
-10% 2
0% 3
10% 4
11% 5
Then the formula in cell B1 on the 'calculations' tab would be something like:
=arrayformula({"Con Potential";iferror(vlookup(vlookup(A2:A,'Extracted data'!A:D,4,0),Ranges!A:B,2,1),)})
Delete all data below cell B1 for the arrayformula to work correctly.
The second vlookup references col D on the 'Extracted data' tab because that is the percentage I think you are comparing? If not, alter 4 in the vlookup to another column.
If it helps, please see:
https://stackoverflow.com/help/someone-answers
NB: In place of Ranges!A:B you could use a fixed array:
=arrayformula({"Con Potential";iferror(vlookup(vlookup(A2:A,'Extracted data'!A:D,4,0),{-10,1;-0.1,2;0,3;0.1,4;0.11,5},2,1),)})
If you want to temporarily see the fixed array in case you want to edit any values, place this in a cell somewhere out of the way:
={-10,1;-0.1,2;0,3;0.1,4;0.11,5}
, is used to bump to a new column, ; is used as a return.
Relevance
Looking at 'Relevance' lookup from 'Position Delta' and this table in your sheet:
Since a 'position delta' value of 10 cannot both have a relevance of 5 and 4, I've made the assumption that 10 gets 5. If that is incorrect, then I'll adjust the boundaries.
Add this to cell C1 on the 'calculations' tab (clearing all cells below):
=arrayformula({"Relevance";iferror(vlookup(vlookup(calculations!A2:A,'Extracted data'!A:D,3,0),{0,5;11,4;21,3;31,2;41,1;51,0},2,1),)})
The fixed array {0,5;11,4;21,3;31,2;41,1;51,0} has these values:
0 5
11 4
21 3
31 2
41 1
51 0
If you need to change the boundaries so 10 is a 4, not 5, then change the vlookup to use this fixed range {0,5;10,4;20,3;30,2;40,1;50,0}:
0 5
10 4
20 3
30 2
40 1
50 0
vlookup is incremental and anything up to 11 will get 5, then 11 to 20 will get 4, 21 to 30 will get 3 and so on.
,1) in the vlookup at the far right gets the nearest value match until 'position delta' has reached the next boundary.

how to use countif with array excel 2016?

In an excel spreadsheet I have multiple sites with 2 rows of data for each site (A, B). There are 25 observations (columns 1 - 5) for each site. I would like to count the number of cases for each site where A + B > 0.
For example,
Site1 A 0 0 1 2 0
Site1 B 1 0 1 0 0
the count would be 3 (that is A+B >0 for 3 cases in site 1). I could add a third line for each site to get the sum and then count the number of cells > 0, but I am not interested in adding an additional line for each site. I have not found an array formula that works for this situation.
=SUM(--((C1:G1+C2:G2)>0))
This is an array formula.
In older versions of Excel, you can use:
=SUMPRODUCT(--((C1:G1+C2:G2)>0))
Or you can enter the original as an array formula
To enter/confirm an array formula, hold down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula seen in the formula bar.

How to compare and add elements of an array

I'd like to take a single array lets say 3x5 as follows
1 3 5
1 3 5
2 8 6
4 5 7
4 5 8
and write a code that will create a new array that adds the third column together with the previous number if the numbers in the first and second columns equal the numbers in the row below it.
since the first two values in row 1 and 2, then add the third elements in row 1 and 2 together
so the output from the array above should look like this
1 3 10
2 8 6
4 5 15
The function accumarray(subs,val) accumulate elements of vector val using the subscripts subs. So we can use this function to sum the elements in the third column having the same value in the first and second column. We can use unique(...,'rows') to determine which pairs of value are unique.
%Example data
A = [1 3 5,
1 3 5,
2 3 6,
4 5 7,
4 5 7]
%Get the unique pair of value based on the first two column (uni) and the associated index.
[uni,~,sub] = unique(A(:,1:2),'rows');
%Create the result using accumarray.
R = [uni, accumarray(sub,A(:,3))]
If the orders matters the script would be a little bit more complex:
%Get the unique pair of value based on the first two column (uni) and the associated index.
[uni,~,sub] = unique(A(:,1:2),'rows');
%Locate the consecutive similar row with this small tricks
dsub = diff([0;sub])~=0;
%Create the adjusted index
subo = cumsum(dsub);
%Create the new array
R = [uni(sub(dsub),:), accumarray(subo,A(:,3))]
Or you can get an identical result with a for loop:
R = A(1,:)
for ii = 2:length(A)
if all(A(ii-1,1:2)==A(ii,1:2))
R(end,3) = R(end,3)+A(ii,3)
else
R(end+1,:) = A(ii,:)
end
end
Benchmark:
With an array A of size 100000x3 on the mathworks live editor:
The for loop take about 5.5s (no pre-allocation, so it's pretty slow)
The vectorized method take about 0.012s

Count based on column and row

I can't seem to find something quite like this problem...
I have an array table where each row contains a random assortment of numbers 1-N
On another sheet, I have a table with column and row headers numbered 1-N
I want to count how many rows in the array contain both the column and row headers for a given cell in the table. Since countifs only reference the current cell in the specified array, they don't seem to be working in this scenario.
Example array:
A B C D
1 3 5 7
1 2 3 4
2 3 4 5
2 4 6 8
...
Table results (symmetrical about the diagonal):
A B C D E F
. 1 2 3 4 5 ...
1 - 1 2 1 1
2 1 - 2 2 1
3 2 2 - 2 2
4 1 2 2 - 1
5 1 1 2 1 -
Would using nested countifs work?
I don't agree with your results corresponding to 4/2, which surely should be 3, not 2, but this formula, based on the array table being in Sheet1 A1:D4 and the results table being in Sheet2 A1:F6, placed in cell B2 of the latter, should work:
=IF($A2=B$1,"-",SUMPRODUCT(N(MMULT(N(COUNTIF(OFFSET(Sheet1!$A$1:$D$1,ROW(Sheet1!$A$1:$D$4)-MIN(ROW(Sheet1!$A$1:$D$4)),),CHOOSE({1,2},B$1,$A2))>0),{1;1})=2)))
Copy across and down as required.
Note: If your actual table is in fact much larger than that given, it will probably be worth adding a simple clause into the above to the effect that the results for approximately half of the cells are obtained from their symmetrical counterparts, rather than via calculation of this construction, thus saving resource.
Regards

Resources