Excel: Sumproduct, input of array and range not working - arrays

I am trying to do a sumproduct such as
=SUMPRODUCT(A1:A8,{8,7,6,5,4,3,2,1})
but it give #VALUE! error,
While it is gives a valid value for
=SUMPRODUCT({8,7,6,5,4,3,2,1},{8,7,6,5,4,3,2,1})
Or
=SUMPRODUCT(A1:A8,A1:A8)
According to my understanding it only works if all inputs are of Range or Array type, but not when there are both. Is there any way to make it work?

or you can use ; for vertical array :
=SUMPRODUCT(A1:A8,{8;7;6;5;4;3;2;1})
{8,7,6,5,4,3,2,1} is horizontal array similar to horizontal range like A1:H1 which will not work well with the vertical range A1:A8
I can't test it, but another way can be with the Row function:
=SUMPRODUCT(A1:A8,9-Row(A1:A8))

=SUMPRODUCT(TRANSPOSE(D3:D10),{8,7,6,5,4,3,2,1})
EDIT: Above code works if entered as array forumla
=SUMPRODUCT(D3:D10,TRANSPOSE({8,7,6,5,4,3,2,1}))
works without entering as array formula.

Related

How can I auto fill a formula with dynamic value in array in the same cell?

so I have a formula inside an array that looks like this
={CRYPTOFINANCE("kraken:"&ARL8&"/USD", "price_history", "10d")}
I want to auto fill the array with the same function but with the 'ARL8' reference in an ascending order, just like auto filling cells, but keep it all in one cell it should look like this...
={CRYPTOFINANCE("kraken:"&ARL8&"/USD", "price_history", "10d") ; CRYPTOFINANCE("kraken:"&ARL9&"/USD", "price_history", "10d") ; CRYPTOFINANCE("kraken:"&ARL10&"/USD", "price_history", "10d")} ,SUBSTITUTE(TRANSPOSE(SPLIT(REPT(12,1050),2)),1,"MISTAKE")) etc.
the thing is that I have about 1000 values from ARL8 reference to ARL1008 reference so it will take really long for me to write it all manually, so is there a way I can keep the rest of the function but have the ARL cell reference dynamically written while being able to specify the length of the array?
It's something I am struggling for a while now and still couldn't find a way so I really appreciate if you could explain the solution as well, and if there is more info I can give let me know, and thanks.
I tried a few things but they gave me different errors I didn't know how to solve and others just didn't work, I will just put it in case it helps
=ARRAYFORMULA(CRYPTOFINANCE("kraken:"&ARL8:ARL1008&"/USD", "price_history", "10d"))
the error it gave me is - "error Attribute price_history isn't supported in batch formulas"
try:
=BYROW(ARL8:ARL20, LAMBDA(x, CRYPTOFINANCE("kraken:"&x&"/USD", "price_history", "10d"))
update
you can generate a formula with a formula like:
={""; INDEX("=ARRAYFORMULA({SPLIT(""Exchange,Base,Quote,Time,Open,High,Low,Close,Quote Volume,Base Volume"", "","")"&
QUERY(";QUERY(TO_TEXT(CRYPTOFINANCE(""kraken:""&"&C2&
SEQUENCE(C3, 1, C4)&"&""/USD"",""price_history"",""10d"")), ""offset 1"", )",,9^9)&"})")}
demo sheet

Using an array formula in Excel with sumif

I'm trying to make an array formula work to sum data based on vertical and horizontal criteria in Excel. Here is the formula I've tried, but it's returning a #NAME? error.
=arrayformula(SUM(IF('By day'!$C$3:$GB$3='By week'!C$1,IF('By day'!$B$4:$B$395='By week'!$A3,'By day'!$C$4:$GB$395))))
Can anyone suggest what I need to change to make this work? I'm new to array formulas so any help would be greatly appreciated. I tried just pressing Enter and pressing Ctrl + Enter, and neither worked.
I also tried this, which is correctly summing based on the vertical range ('By day'!$B$4:$B$395) but doesn't take account of the horizontal range ('By day'!$C$3:$GB$3) - i.e. it simply sums all columns that match the vertical range:
=SUM(IF(('By day'!B4:B401='By week'!A3)+('By day'!C3:GB3='By week'!B1),'By day'!C4:GB235,0))
Thanks in advance!

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

how to modify an array value with given index?

I want to modify an array cell, which I can do when I know the cell as a number. However here my cell position is given by $i.
pomme[`${i}`]=""
I tried without the `` and it doesn't work either?
How am I suppose to do it?
You don't need the quotes. Just use ${i}, or even $i:
pomme[${i}]=""
Or
pomme[$i]=""

Matlab making new cell array from another cell array fileds

I have a cell array thiscells:
thiscells <1*40 cell>
eacharray of thiscells is 280*3
I have a function (TheFunction) that gets input of cell array that each array is X*2
How can I send a cell array to TheFunction that contain only columns1,3 from each array?
I tried
TheFunction(thiscells{:}(:,1:2)) % tring to send only columns 1,2
But it didn't work
Edit:
working with (but still looking for faster way to do so):
What is did so far
TheFunction(makingNewCells(thiscells,2));
when
makingNewCells:
[newCells]=makingNewCells(oldCells)
for ii=1:max(size(oldcells))
newCells=oldCells(:,[1 column]);
end
This question and its answers may be of help - thiscells{:}(:,1:2) is already failing without the call to Thefunction, if I am not mistaken.
For cells, the problem is even worse: add to this impossibility to double-subref that even if it did what you intended it to do, this passes 40 arguments (which are all 280x2 vectors) to Thefunction. I doubt there are (m)any legitimate uses for a function that takes 40 arguments of the same type, instead of having those wrapped in a cell. I would probably try doc cellfun first.
EDIT: just to be clear, I suggest changing Thefunction so that it accepts a cell of 40 matrices of size 280x2), then using
cell_of_twocolumned_arrays = cellfun(#(x)x(:,[1 2]),thiscells)

Resources