Summing one dimension of a multidimension wave with MartixOP

Hi everybody,

I am learning to work more and more with MatrixOP to replace loops in my code. Currently I want to sum the columns of a 3-dimensional wave. In other words, when I have a wave with dimensions [Rows][Columns][Layers] I want to get the result with dimensions [Rows][Layers] where each row contains the sum over all columns in the respective layer (i hope this makes sense). Here is what I have so far:

Function ReduceCols(in)
    wave in
    Variable Rows   = DimSize(in, 0)
    Variable Columns    = Dimsize(in, 1)   
    Variable Layers = Dimsize(in, 2)

    Make/D/FREE/N=(Rows,Layers) Out                 // placeholder with reduced dimensions
    Variable i
    for (i = 0; i< Layers; i+=1)
        MatrixOP/FREE work=sumRows(layer(in,i))
        Out[][i] = work[p]
    Redimension/N=(Rows,Layers) in
    in = out[p][q]

This works, but I wonder if I could get rid of the loop completely and make MatrixOP do all the work. There seems to be no way of indexing the layer() part within MatrixOP so I guess I would have to take another approach. Any suggestions are welcome. 


I believe you might get away with:

Function ReduceCols(in)
    wave in
    MatrixOP/FREE temp = SumRows(in)
    MatrixOP/O in = TransposeVol(temp,1)

(edit: made it a complete function)

Awesome! And this solution is even Igor 6 compatible (without the layer() part). I do not fully understand how sumRows() works on a 3D wave (I assume it works on the two highest dimensions, in this case [columns] x [layers]?). Anyway. it works great and I learned something new. Thanks a lot!

In reply to by chozo

chozo wrote:
I do not fully understand how sumRows() works on a 3D wave (I assume it works on the two highest dimensions, in this case [columns] x [layers]?).

As far as I understand SumCols, SumRows and SumBeams just sum the elements along x, y and z, respectively. For a 3D wave you'll end up with a single row (with z layers), a single column (with z layers, as in your case) or a single 2D layer.


The behavior of MatrixOp with respect to layers is described in the "Wave Parameters" section of the voluminous MatrixOP help topic:

"MatrixOp was designed to work with 2D waves (matrices), but also works with 1D, 3D and 4D waves. A 1D wave is treated like a 1-column matrix. 3D and 4D waves are treated on a layer-by-layer basis, as if each layer were a matrix."

Yes, the MatrixOP help sooner or later will be its own book, I guess. I probably tripped over the the function names and the direction of rows and columns in a table vs. in a graph. In the end, sumRows() is the sum of all column values in each row, just like shown in the formula. I would have called this one rather sumCols() instead, i.e., a 'sum of columns'. Anyway, now I understand and all is fine. I will try to use MatrixOP as more and more replacement for loopy wave assignments in the future, so this helps a lot.

Choosing the right terms is often tricky. I think you can justify the SumRows name if you think of it as "sum along rows".




Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More