Adding all rows of specific columns in a 2D wave

Problem statement: 

I have a 2D wave: datawave[22][19]. 

I also have three variables var1,var2 and var3. 

var1 needs to be sum of all rows in the first column of datawave.

var2 needs to be sum of all rows in second, third and fourth columns. 

var3 is sum of all rows going from column 5 to column 19. 

------------------------

I was trying to do: 

//way1::::  
MatrixOp/O var1=sum(datawave[p][0])

//and also another way I tried:
//way2:::
variable var1
var1=sum(datawave[][0])

 the error I get with way 1 is "operator/operand mismatch" highlighting [0]. 

with way 2, I get "expected right parenthesis" highlighting "[" of the row counter. 

 

I request you to please help me understand what I'm doing incorrectly. Much thanks in advance! 

Hi,

 

Take a look at SumDimension.  This will reduce the 2D wave to a 1D of sums.  You can then more easily handle the calculations with either selecting a row value or do a Sum(wave name,x1,x2)  to sum a range of positions (columns in your 2D wave.

 

Andy

Your "way1" error is that you are using [p][0].  MatrixOp does not work with p's and q's.

Your "way2" error is that the sum() function accepts ranges but is not multi-dimensional aware.  So you could use it to sum certain cols if you removed any wave scaling and computed straight index for the point.

Using MatrixOP is probably the way to go.  For simple stuff, e.g.,

MatrixOP/O w1=sum(col(datawave,0))

Remember that the result is a one point wave, not a variable.

Now to handle a sum of arbitrary user specified cols start by creating an appropriate flag wave and using it to zero all the entries that are not wanted.  Then use MatrixOP to compute the sum:

Make/n=22/FREE flagWave=0
// as an example
flagWave[4,18]=1  // assuming zero base
MatrixOP/o w3=sum(scaleCols(dataWave,flagWave))

I hope this helps,

 

A.G.