Is it possible to perform an arbitrary calculation (eg. A2*B2) on a set of rows and obtain the cumulative sum along the way using ARRAYFORMULA? For example, in the following sheet we have numbers (column A), multipliers (column B), the result of multiplying them (column C), and a cumulative tally (column D):
| A B C D E F
-------------------------------------------------------------------------------
1 | number multiplier result cumulative array formula array formula sum?
2 | 3 4 12 12 12
3 | 2 4 8 20 8
4 | 10 1 10 30 10
5 | 7 9 63 93 63
I can use ARRAYFORMULA in cell E2 (specifically, ARRAYFORMULA(A2:A5*B2:B5)) to do the multiplication. Is it possible to use ARRAYFORMULA (or alternative tool) in cell F2 to show the cumulative total?
use:
=ARRAYFORMULA(IF(A2:A="",,MMULT(TRANSPOSE((ROW(A2:A)<=
TRANSPOSE(ROW(A2:A)))*A2:A*B2:B), SIGN(B2:B))))
Calculate the cumulative sum with the SCAN and LAMBDA functions:
=SCAN(0, F5:F, LAMBDA(accumulated_value, cell_value, accumulated_value + cell_value))
This will run faster as it runs with linear complexity (O(N)) compared to the ARRAYFORMULA solution, which runs in quadratic time (O(N**2)).
Where:
0 is the initial value of the cumulative sum
F5:F is the range to sum over
LAMBDA(accumulated_value, cell_value, accumulated_value + cell_value)) is the function that calculates the sum at each cell
Sample File
Related
I have a constant number X. I also have two numbers that add up to it. How can I make it so that if I change one number, the other number automatically changes so that it still adds up to X.
I have tried to take subtract the one number from X and add it to the other number, but instead I got two numbers in the thousands.
Assuming your constant value is 10, you can set this in a cell and make all your other calculations based on it.
For example, you can have cell C2 containing your constant, in this example, 10
Then in C4 you can have the number which you change, and the value of C5 will be equal to the value of the constant minus the value in C4.
You can then finally do your sum wherever you want, adding up the values of C4 and C5.
Here's an example Spreadsheet:
Untitiled spreadsheet โ
File Edit View Insert Format Data Tools Extensions Help Last edit was 2 minutes ago
โถ โท ๐ถ โฎท | 100%โฏ | $ % .0 .00 123โฏ | Default(Ro... โฏ | 10 โฏ | B | I | S | A |โฏ|โฐ
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
1
2
Contsant:
10
3
4
Number 1:
3
5
Number 2:
=(C2 - C4)
6
7
Sum:
=(C4 + C5)
8
I have been given matrices filled with alphanumerical values excluding lower case letters like so:
XX11X1X
XX88X8X
Y000YYY
ZZZZ789
ABABABC
and have been tasked with counting the repetitions in each row and then tallying up a score depending on the ranking of the character being repeated. I used {โบ (โขโต)}โธยจ โ m to help me. For the example above I would get something like this:
X 4 X 4 Y 4 Z 4 A 3
1 3 8 3 0 3 7 1 B 3
8 1 C 1
9 1
This is great but now I need to do a function that would be able to multiply the numbers with each letter. I can access the first matrix with โ but then I am completely lost on how to access the other ones. I can simply write โw[2] and โw[3] and so forth but I need a way to change every matrix at the same time in one function. For this example, the array of the ranking is as follow: ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210 so for the first array XX11X1X
which corresponds to:
X 4
1 3
So the X is 3rd in the array so it corresponds to a 3 and 1 is 35th so it's a 35. The final scoring would be something like (3ร104)+(35ร103). My biggest problem is not necessarily the scoring part but being able to access each matrix individually in one function. So for this nested array:
X 4 X 4 Y 4 Z 4 A 3
1 3 8 3 0 3 7 1 B 3
8 1 C 1
9 1
if I do arr[1] it gives me the scalar
X 4
1 3
and โด arr[1] gives me nothing confirming it so I can do โarr[1] to get the matrix itself and have access to each column individually. This is where I'm stuck. I'm trying to write a function to be able to do the math for each matrix and then saving those results to an array. I can easily do the math for the first matrix but I can't do it for all of them. I might have made a mistake by making using {โบ (โขโต)}โธยจ โ m to get those matrices. Thanks.
Using your example arrangement:
โ โ arranged โ โฝ โD , โA
ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210
So now, we can get the index values:
1 โท m
XX11X1X
โช 1 โท m
X1
arranged โณ โช 1 โท m
3 35
While you could compute the intermediary step first, it is much simpler to include most of the final formula in in Key's operand:
{ ( arranged โณ โบ ) ร 10 * โขโต }โธยจ โm
โโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ30000 35000โ30000 28000โ20000 36000โ10000 290 280 270โ26000 25000 240โ
โโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
Now we just need to sum each:
+/ยจ { ( arranged โณ โบ ) ร 10 * โขโต }โธยจ โm
65000 58000 56000 10840 51240
In fact, we can combine the summation with the application of Key to avoid a double loop:
{ +/ { ( arranged โณ โบ ) ร 10 * โขโต }โธ โต}ยจ โm
65000 58000 56000 10840 51240
For completeness, here is a way to use the intermediary result. Let's start by working on just the first matrix (you can get the second one with 2โ instead of โ โ for details, see Problems when trying to use arrays in APL. What have I missed?):
โ{โบ (โขโต)}โธยจ โm
X 4
1 3
We can insert a function between the left column elements and the right column elements with reduction:
{โบ 'foo' โต}/ โ{โบ (โขโต)}โธยจ โm
โโโโโโโโโโโฌโโโโโโโโโโ
โโโโฌโโโโฌโโโโโโฌโโโโฌโโโ
โโXโfooโ4โโโ1โfooโ3โโ
โโโโดโโโโดโโโโโโดโโโโดโโโ
โโโโโโโโโโโดโโโโโโโโโโ
So now we simply have to modify the placeholder function with one that looks up the left argument in the arranged items, and multiplies by ten to the power of the right argument:
{ ( arranged โณ โบ ) ร 10 * โต }/ โ{โบ (โขโต)}โธยจ โm
30000 35000
Instead of applying this to only the first matrix, we apply it to each matrix:
{ ( arranged โณ โบ ) ร 10 * โต }/ยจ {โบ (โขโต)}โธยจ โm
โโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ30000 35000โ30000 28000โ20000 36000โ10000 290 280 270โ26000 25000 240โ
โโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
Now we just need to sum each:
+/ยจ { ( arranged โณ โบ ) ร 10 * โต }/ยจ {โบ (โขโต)}โธยจ โm
65000 58000 56000 10840 51240
However, this is a much more circuitous approach, and is only provided here for reference.
I have table like this
A | B | C | Val
5 | 5 | 5 | 10
5 | 4 | 5 | 20
4 | 4 | 4 | 5
3 | 3 | 4 | 7
Is there any way sumifs can use criteria based on a set of numbers?
E.g. I want to sum the Val column based on these cells:
A: 5
B: 4,5 -> means get 4 or 5
C: 5
Then the result is 10 + 20 = 30
A: 3,4,5
B: 3,4
C: 4
Then the result is 5 + 7 = 12
You can combine SUMPRODUCT and MMULT functions to get dynamic formula:
=SUMPRODUCT(
($D$2:$D$5)
*MMULT(--($A$2:$A$5=$I$2:$K$2),ROW($A$1:$A$3)^0)
*MMULT(--($B$2:$B$5=$I$3:$K$3),ROW($A$1:$A$3)^0)
*MMULT(--($C$2:$C$5=$I$4:$K$4),ROW($A$1:$A$3)^0)
)
There's no simple way to do it with SUMIFS as it does not let you express OR conditions with Array literals like in Excel.
However, Google Sheets does offer the QUERY function.
Assume that criteria for A is in H1:1, B is in H2:2, C is in H3:3:
=QUERY(A:D,"select sum(D) where
(A="&TEXTJOIN(" or A=",1,H1:1)&") and
(B="&TEXTJOIN(" or B=",1,H2:2)&") and
(C="&TEXTJOIN(" or C=",1,H3:3)&")")
This gives you a header too. To get just the sum you can INDEX it:
=INDEX(
QUERY(A:D,"select sum(D) where
(A="&TEXTJOIN(" or A=",1,H1:1)&") and
(B="&TEXTJOIN(" or B=",1,H2:2)&") and
(C="&TEXTJOIN(" or C=",1,H3:3)&")")
,2)
With this, you don't have to worry about headers or the number of criteria.
For a way to do it without QUERY see #basic's solution.
I think you can do it with relatively simple SUMPRODUCT(MMULT(... as shown here on this sheet.
=SUMPRODUCT(E4:E,MMULT(N(REGEXMATCH(TO_TEXT(B4:D),B2:D2)),SEQUENCE(3,1,1,0))=3)
Note the use of the number 3 in two places which is indicative of how wide your data range is. It can be made dynamic, but it's easier to see how the formula works with the 3s as "hard" numbers for now.
Example
3 2 5 5
a b c d
Joining first two
5 | 5 5
3 2 | c d
a b |
I have to put the new tree of five into the queue
Am I obligated to put it in the end like this:
5 5 5
c d / \
3 2
a b
Or can I put it in the beginning:
5 5 5
3 2 c d
a b
Or even in the middle of 'c' and 'd'
Is it my choice or is there a rule?
It's not your choice, the Queue needs to be sorted at all times (by it's number of occurrences and in case of equal number of occurrences by the depth of the tree). So it needs to be inserted where it belongs into the order.
This is needed to pick the sub-trees with the least amount of occurrences and if there is choice the most shallow one of them by simply pop-ing them.
If you simply resort after every insertion (this is inefficient and should not be done) the position obviously doesn't matter.
Yes, it's your choice. Whichever way you will get an optimal Huffman code, even though two resulting codes can be manifestly different.
You can get:
a - 00
b - 01
c - 10
d - 11
or you can get:
a - 111
b - 110
c - 10
d - 0
Now if I multiply the number of bits in each symbol times the number of occurrences, I get for the first code: 2*3 + 2*2 + 2*5 + 2*5 = 30 bits. For the second code: 3*3 + 3*2 + 2*5 + 1*5 = 30 bits. So both codes will code the original message to exactly 30 bits.
I have written this formula below. I do not know the correct part of this formula that will add the numbers I have in Column AB2:AB552. As it is, this formula is counting the number of cells in that range that has numbers in it, but I need it to total those numbers as my final result. Any help would be great.
=COUNTIFS(Cases!B2:B552,"1",Cases!G2:G552,"c*",Cases!X2:X552,"No",**Cases!AB2:AB552,">0"**)
Assuming you don't actually need the intermediate counts, the sumifs function should give you the final result:
=SUMIFS(Cases!AB2:AB552,Cases!B2:B552,1,Cases!G2:G552,"c",Cases!X2:X552,"No",Cases!AB2:AB552,">0")
Testing this with some limited data:
Row B G X AB
2 2 a No 10
3 1 c No 24
4 2 c No 4
5 1 c No 0
6 1 a Yes 9
7 2 c No 12
8 2 c No 6
9 2 b No 0
10 1 b No 0
11 1 a No 10
12 2 c No 6
13 1 c No 20
14 1 c No 4
15 1 b Yes 22
16 1 b Yes 22
the formula above returned 48, the sum of AB3, AB13, and AB14, which were the only rows matching all 4 criteria