Matrices in Swift - arrays

Since I am quite new with Swift, I was reading about Subscripts in Swift. It gave this example under Subscript Options: theExample
My question is how is the formula, grid[(row * columns) + column], is suppose to correspond to the right value in the grid array because when I calculate it it corresponds to the wrong value. Somebody please help.

0 1 2 3 4
--------------------
0 | 0, 1, 2, 3, 4,
1 | 5, 6, 7, 8, 9,
2 | 10, 11, 12, 13, 14
You have columns = 5.
If you want to get 8, row = 1 and column = 3.
row * columns + column
1 * 5 + 3 = 8.

Related

Sum values using Arrays and INDEX

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].

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 missing data in a data set with constant values

I have a data set like the following:
x= [1, 4, 10]
y= [10, 20, 30]
(x and y are value pairs, i.e. (1,10), (4,20), (10,30))
I would like to fill the x values gaps and having constant values for y until the next known value pair comes.This should be done between each value pair, i.e. between (1,10) and (4,20) and then again between (4,20) and (10,30).
Input:
x=[1, 4, 10];
y=[10, 20, 30];
Output:
xi= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
yi= [10,10, 10, 20, 20, 20, 20, 20, 20, 30];
How can Matlab solve this for me?
Assuming ascending order of elements in x, this could be one approach based on diff & cumsum -
%// Sample inputs
x=[1, 4, 10]
y=[-2, 5, -3]
xi = min(x):max(x)
yi = zeros(1,numel(xi))
yi(x) = diff([0 y])
yi = cumsum(yi)
Sample run -
x =
1 4 10
y =
-2 5 -3
xi =
1 2 3 4 5 6 7 8 9 10
yi =
-2 -2 -2 5 5 5 5 5 5 -3
Customary bsxfun solution to get yi -
lens = [diff(x) 1];
yi = nonzeros(bsxfun(#times,bsxfun(#ge,lens,[1:max(lens)]'),y)).'
Assuming that x always starts with a 1 and finishes with the final length of xi, this will work:
xi=1:x(end)
yi=y(arrayfun(#(xi)find(x<=xi,1,'last'),xi))

Given an unsorted array, Find the maximum subtraction between two elements in the array

I've got this question from an Interview in Microsoft: Given an unsorted array, Find the maximum subtraction between two elements in the array is a way that:
(Index1, Index2) = arr[Index2] - arr[Index1]. Index1<Index2.
Example:
given the array: [1, 5, 3, 2, 7, 9, 4, 3] -> Output: (1,9)=8.
given the array: [4, 9, 2, 3, 6, 3, 8, 1] -> Output: (2,8)=6.
The naive solution works in O(n^2) times: Scan the first index for subtraction with all other indexes and save the max value, Continue to the next index and so on.
Is there any way to optimize this?
Fairly simple when you write it down. Rephrasing the problem, you want to find the largest element to the right of each element. Now given the first example, this is:
[1, 5, 3, 2, 7, 9, 4, 3]
=>
[9, 9, 9, 9, 9, 4, 3]
Now, notice the maximums array is just the cumulative maximums from the right. Given this property it is easy to construct an O(n) time algorithm.
Implementation in python:
def find_max(xs):
ys = []
cur_max = float('-inf')
for x in reversed(xs):
cur_max = max(x, cur_max)
ys.append(cur_max)
ys = ys[::-1][1:]
return max(y - x for x, y in zip(xs, ys))
We can also construct the maximums array lazily, doing so gives us O(1) memory, which is the best possible:
def find_max(xs):
cur_max = float('-inf')
cum_max = xs[-1]
for i in range(len(xs) - 2, -1, -1):
cur_max = max(cur_max, cum_max - xs[i])
cum_max = max(cum_max, xs[i])
return cur_max
I think this is correct and O(nlogn): Split in the middle and select from right the max, from left the min value. Also split the the other 2 quarters, if one of them gives bigger result continue on that branch recursively.
Second example:
4, 9, 2, 3| 6, 3, 8, 1 -> 2 and 8
4, 9| 2, 3, 6, 3, 8, 1 -> 4 and 8
4, 9, 2, 3, 6, 3| 8, 1 -> 2 and 8
So working on the right split:
4, 9, 2, 3, 6, 3, 8| 1 -> 2 and 1
Selecting the 2 and 8 option. It also works for the first example.

Adding ints from array

Im taking CompArch for the first time ever. I have to print out an array
list: .word 3, 0, 1, 2, 6, -2, 4, 7, 3, 7
I managed to print it out in two rows
R1 3 0 1 2 6
R2 -2 4 7 3 7
How can i create a loop or code to add all the numbers in the first row. Or in other words the first 5 ints from the array. Thanks for your help in advance.

Resources