# Combining waves I'm curious if there is an easy/elegant approach to this problem. I have two or more waves containing different numbers of sequential threshold crossings, some of which may be the same crossing value across waves, but which may happen at different point values in each wave, such as the following:

Wave "A"
4
5
7
9

Wave "B"
1
3
4
8

Wave "C"
7
8
9

What I need to do is combine these waves, so the numbers from them are sequential, and not duplicated. So for instance, the result of the above three waves would be the following:

1
3
4
5
7
8
9

Is there some matrix/array math operation that can do this in Igor, or some built-in function? If not, then is there some programming technique that can accomplish this without resorting to a number of nested loops and conditionals? Ideally I'd like to be able to take an arbitrary number of such waves in a list string (ie, "WaveA;WaveB;WaveC;WaveD") as the input to a function, and have this function output a single wave combined in the method stated above.

It seems simple, but every time I wrap my head around it I get stuck on some impasse. Currently I can create a function that does this comparison for two waves, and then simply loop it in another function to compare its result to additional waves, but I'm hoping there is a more elegant solution (kind of like the "prime" notation in Matlab to transpose a matrix, instead of having to create a new matrix, then loop through the first and assign values to the second, etc.).
Off the top of my head, I would say … concatenate the waves, sort the result, and eliminate duplicates. The first two steps are one-line operations or functions. The last step could be done easiest I think in a loop going backward over the wave with an if/then comparison and delete points. Someone clever may have a quicker answer without loops.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
jjweimer wrote:
... may have a quicker answer without loops.

Yes, there is a way in two lines of code. Assuming wave0 contains the concatenated and sorted data, then:
wave0[0, DimSize(wave0, 0) - 2] = wave0[p] != wave0[p + 1] ? wave0[p] : NaN
WaveTransform/O zapNaNs wave0

Hope this helps,
Kurt
KurtB wrote:
jjweimer wrote:
... may have a quicker answer without loops.

Yes, there is a way in two lines of code. Assuming wave0 contains the concatenated and sorted data, then:
wave0[0, DimSize(wave0, 0) - 2] = wave0[p] != wave0[p + 1] ? wave0[p] : NaN
WaveTransform/O zapNaNs wave0

Hope this helps,
Kurt

what is [p] here?
--Now let's all stay positive, and do some science.
DimensionalDifficulties wrote:
what is [p] here?

In a wave assignment statement p takes on the value of the row number. In the statement example given, there is an implicit loop such that it takes on each row number in turn from the first row (row 0) to the penultimate row (row N-2, where N is the total number of rows in the wave - note the last row in the wave is row N-1).

Type the following in the command line for more details:
DisplayHelpTopic("Waveform Arithmetic and Assignment")