Sum values using Arrays and INDEX - arrays

I have the following sample sheet:
1/A B C D E F G H I J
2
3 Points 8 4 2 1
4
5 Values 1 2 3 4 4 3 1 2
I'm trying to sum the 'Points' based upon the array index from the 'Values'.
My expected result from this is: 30
Here is my formula:
{=SUM(INDEX($C$3:$F$3,1,C5:J5))}
For some reason though, this only returns the first value of the array, rather than the entire sum.
To clarify, the C# version would be something like:
var points = new int[] { 8, 4, 2, 1 };
var values = new int[] { 2, 4, 3, 1, 2, 4, 2 };
var result = (from v in values
select points[v - 1]).Sum(); // -1 as '4' will crash, but in Excel '4' is fine
Edit: Adding further clarifying example
Another example to clarify:
Points is the array. The 'values' represents the index of the array to sum.
The example above is the same as:
=SUM(8, 4, 2, 1, 1, 2, 8, 4)

INDEX will never take its row or column parameters from arrays and then perform multiple times within one array formula contained in one cell. For this OFFSET will be needed.
Either
{=SUM(N(OFFSET($C$3,,C5:J5-1)))}
as an array formula.
Or
=SUMPRODUCT(N(OFFSET($C$3,,C5:J5-1)))
as an implicit array formula without the need for [Ctrl]+[Shift]+[Enter].

Related

remove all negative elements from an array buffer of integers in scala

I have a question about coding. There are similar types of questions in the database which I came across but none of them clears my doubt. I am going thru the book of "Scala for Impatient". The code below removes negative elements from the Array and gives positive elements as output
val a = ArrayBuffer(-1, 1, 0, -2, -1, 2, 5, 6, 7)
val positionsToKeep = for (i <- a.indices if a(i) >= 0) yield i
for (j <- positionsToKeep.indices) a(j) = a(positionsToKeep(j))
a.trimEnd(a.length - positionsToKeep.length)
It gives the output as (1,0,2,5,6,7) removing all negative elements.
I am unable to understand line 3 & 4.
for (j <- positionsToKeep.indices) a(j) = a(positionsToKeep(j))
a.trimEnd(a.length - positionsToKeep.length)
I'm scratching my head since 2 days on these 2 lines but can't give up and I finally posting it here seeking some help.
As a is a bufferArray so we can change the values of the array a.
Line 3:
Line 3 is populating or you can say updating the value of positionToKeep into a.
a(j) = positionToKeep(j)
// which is running like this
// a(0) = positionToKeep(0)
// a(1) = positionToKeep(1) .... and so on
Now what will happen after populating all the values of positionToKeep into a there might be the case some older values remains untouched. Line four is deleting or dropping these elements. In the case when we have all the positive values in array a line four has like no use but when the length of a is greater then positionToKeep then we need line 4.
Line 4: consider the scenario
val a = Array(1, 2, 3, 4, 5, 6)
Then our positionToKeep will have all the element and the length of both the array will be equal.
val positionToKeep = Array(1, 2, 3, 4, 5, 6)
In this case line four trimEnd(0) because length of a and positionToKeep are equal.
val a = Array( 1, 2, 3, 4, -5, -6, 8, 9, -3)
In this case we will have Array(1,2,3,4,8,9) in positionToKeep
In line 3 we will update array a and after updating before line four this is how our array a will look like.
Array(1,2,3,4,8,9,8,9,-3) as we need values only up to length 6 as we have only 6 positive values. We need to drop last 3 element that what is tripEnd doing for us.

Having issues subracting two arrays with 2400 rows and 1 colum each

Program: MATLAB 2013
Dimension of RANKPosY and RANKPosZ: 2402 rows by 1 column
Variable class: 2402 x 1 (cell)
My goal is to subtract RANKPosY - RANKPosZ, but when I compile and run I get the following error
RANKPosY - RANKPosZ
ERROR MESSAGE: **Undefined function 'minus' for input arguments of type
'cell'.**
I read other stackoverflow solutions and those have not worked still get the same error as before.
You will want to use cellfun to apply minus on the cell arrays:
a = {1, 3, 5, 7};
b = {1, 2, 3, 4};
cellfun(#minus, a, b);
ans =
0 1 2 3

Matlab: creating a bigger array using a smaller array

I have two giant array which looks like:
A = [11, 11, 12, 3, 3, 4, 4, 4 ];
B = [ 12, 4; 3, 11; 11, 1; 4, 13 ];
I want to create an array which takes values from B and column 1 from A to look like:
C = [ 11, 1; 11, 1; 12, 4; 3, 11; 3, 11; 4, 13; 4, 13; 4, 13 ];
I don't want to use for or any other kind of loop to optimize the process.
Sorry for being terse.
I will search each element from column 1 of A in column 1 of B and pick the corresponding column 2 elements from B and create a new array with column 1 elements of A and discovered column 2 elements from B.
What you are doing in this problem is using A and searching the first column of B to see if there's a match. Once there's a match, extract out the row that corresponds to this matched location in B. Repeat this for the rest of the values in A.
Assuming that all values of A can be found in B and that the first column of B is distinct and that there are no duplicates, you can a unique call and sortrows call. The unique call is on A so that you can assign each value in A to be a unique label in sorted order. You would then use these labels to index into the sorted version of B to get your desired result:
[~,~,id] = unique(A);
Bs = sortrows(B);
C = Bs(id,:);
We get for C:
C =
11 1
11 1
12 4
3 11
3 11
4 13
4 13
4 13
Thanks to #rayryeng for clarifying the question to me.
Assuming each element from A is present in column 1 of B:
[~, ind] = max(bsxfun(#eq, A(:).', B(:,1)), [], 1);
C = B(ind,:);
If that assumption doesn't necessarily hold:
[val, ind] = max(bsxfun(#eq, A(:).', B(:,1)), [], 1);
C = B(ind(val),:);
So for example A = [11, 20, 12, 3, 3, 4, 4, 4 ]; would produce
C =
11 1
12 4
3 11
3 11
4 13
4 13
4 13

Filling fixed sized array with either integer 1 or 2 to make a sum X

Let's say I have a fixed sized array. I want to fill the array with either 1s or 2s so that all element sums up to X.
Example:
Required sum = 12
Array size = 7
Possible combinations:
array( 1, 2, 2, 2, 1, 2, 2 ) // sums to 12
array( 1, 1, 2, 2, 2, 2, 2 ) // sums to 12
Find the number of 2's in the array, this number is:
#2's = X - array_size
Chose random arbitrary #2's elements (for example the first elements), and
give them the value 2, the rest of the elements will get the value 1.
Note: it is easy to see that if X < array_size or X> 2*array_size there is no solution to the problem (and obviously the above algorithm will fail)

interpolation of array inside a array

When I was trying to print a specific element in array, I have mistakenly typed the name of the same array inside [ ] as element and got some output as shown. I thought that it is taking the size of the array and printing that number of characters, which was proven to be wrong by the output.
#array = (0..10, 12);
print "#array[#array]";
prints
Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
0 1 2 3 4 5 6 7 8 9 10
I went one step ahead and edited code as shown below
print "#array[#array[#array]]";
output as: `0 1 2 3 4 5 6 7 8 9 10 0` Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
Now I tried to put a number inside the second array instead of again giving array name as below.
print "#array[#array[1,2,3]]";
output as
1 2 3
But no warning message this time.
What it is trying to print? and What is the reason behind this behavior.
You're creating an array slice. Start out by understanding that your array contains elements 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12.
That's a total of 12 elements (there is no value 11, but at index 11 you are storing the value 12).
Now when you use this construct: #array[#array] you're taking a slice, and requesting the values stored in indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 12. There is no element with an index of 12 in your array. The eleventh element has the value "12".
#array[] is array slice notation. For example:
my #array = (qw/apple banana cucumber date/)
#array[1,2] will return a list of (banana, cucumber) (index 1 and index 2 of #array).
Now, #array[#array] tries to take the values of the inner #array and use them as indexes of the outer #array. In your example, #array[#array] is equivalent to #array[0..10, 12]. Since one of the values of the inner array is 12, and the outer array has no index of 12, you get an undefined value warning.

Resources