# ICE element-wise addition of two arrays of different sizes

For example, given the arrays [1,2] and [1,2,3,4], produce this array: [2,3,4,5,3,4,5,6].

In Python, this would look like this:

```a = [1,2]
b = [1,2,3,4]
c = []
for x in a:
for y in b:
c.append( x + y )

print c
#  [2, 3, 4, 5, 3, 4, 5, 6]
```

In ICE, one way to do this would be to add
[1,1,1,1,2,2,2,2]
to
[1,2,3,4,1,2,3,4]

Here’s an ICE tree that does just that: This ICE tree uses the modulo trick, and the scalar-to-integer truncation trick.

You can see a variation of this in this Softimage mailing list post..

# ICE: Doing an element-wise stretch on an array

I’m not sure “element-wise stretch” is the right terminology, but what I mean is suppose you want to build a new array by repeating the original elements N times. For example, suppose you have the array [1,2], and you want to turn it into [1,1,1,1,2,2,2,2].

Here’s a little ICE technique to do just that: This tree relies on Divide by Scalar truncating the scalar result to make it an integer.

# ICE: Finding the array elements that occur the most frequently

Another example usage of Generate Sample Set instead of Repeat. This time, the problem is to find the array element with the most occurrences. This seems kinda long winded (it’s a three-step process), but it does handle the case where you have two or more elements that occur the same number of times. I used a temporary attribute for formatting purposes (so I didn’t have one big long horizontal tree). And I used a compound to encapsulate the bit that takes an array and converts it to a “per generated element” data set: If you want, here’s a compound version.

# ICE: Removing duplicates from arrays

Here’s an ICE tree that removes all duplicate elements from an array. It uses Generate Sample Set, so there’s no repeat nodes. But it relies on the fact that you can feed in an array of indices into Remove from Array, and Remove from Array doesn’t care if that array of indices itself contains duplicate. So, if you plug the array [1,1,1,2,2,2,3,3,3] in the Index port, Remove from Array will nicely remove elements 1, 2, and 3 with no complaints. Unlike some other methods, this works with scalars too: # ICE: adding consecutive elements in an array

I was hoping to do something more complicated, but I ran out of time…

Here’s the exercise: Take an array and build a new array by adding consecutive elements in the original array.
For example, given this array:

`1 3 2 4 7 5 6 9 11 1 3 9`

construct this array:

`1+3, 3+2, 2+4, 4+7, ..., 3+9` Summing up three consecutive elements will hopefully be more interesting…

# Using Generate Sample Set to avoid the Repeat node

Hat tip to Oleg who posted this tip on the mailing list some time ago.

The basic ideas is that instead of looping over an array with a Repeat with Counter, you use Generate Sample Set to get a data set, and you do everything in a per-generated element context. As an example, let’s revisit the problem of taking one array and creating a new array where each element is the sum of the elements before it. The old way, with a Repeat with Counter node, looks like this: Using Generate Sample Set, you can work with a data set and use that to access the array elements: Generate Sample Set is set up to give an exact number of samples: # Boolean operations on ICE arrays

While it is true that non-zero values are considered True, this applies only to input values. ICE nodes that return a boolean value will always return either 1 or 0.

Based on this, you can do an element-wise logical operation on array by summing up the array elements.

If the sum is greater than zero, then you know there was at least one True value, so a logical OR would return True. And if the sum was less than the array size, then at least one boolean element is False, so a logical AND would return False.  # ICE powers of 10 array

In ICE, how would you generate an array like [1, 10, 100, 1000, 10000, 100000, …] without using a Repeat node ?

It might help to look at it this way:

```1
10
100
1000
10000
100000
```

Once you recognize that you are looking at powers of 10, and recall that the basic Math nodes can handle arrays just as well as single values, the rest follows easily: 