Subtract to 0 in a matrix

I want to subtract matrixA from matrixB but any minus values in matrixB to be set to 0. What's the easiest way of doing this in Igor? In matlab I would write:

matrixC=matrixB-matrixA;
matrixC(matrixC<0)=0;

Thanks
jamesbutler01 wrote:
I want to subtract matrixA from matrixB but any minus values in matrixB to be set to 0. What's the easiest way of doing this in Igor? In matlab I would write:

matrixC=matrixB-matrixA;
matrixC(matrixC<0)=0;

Thanks


Try

matrixC = matrixB - matrixA < 0 ? 0 : matrixB - matrixA
jtigor wrote:


Try

matrixC = matrixB - matrixA < 0 ? 0 : matrixB - matrixA


Thanks! That works perfectly. Could you possibly explain the syntax quickly? I don't understand how to use the ? or : and I am struggling to google an explanation for them
An alternative using MatrixOP:
MatrixOP/O matrixC=clip(matrixB-matrixA,0,inf)

jamesbutler01 wrote:
jtigor wrote:


Try

matrixC = matrixB - matrixA < 0 ? 0 : matrixB - matrixA


Thanks! That works perfectly. Could you possibly explain the syntax quickly? I don't understand how to use the ? or : and I am struggling to google an explanation for them


It is a conditional operator. It is like an if-else-endif expression. <expression> ? <true> : <false>
jamesbutler01 wrote:
jtigor wrote:


Try

matrixC = matrixB - matrixA < 0 ? 0 : matrixB - matrixA


Thanks! That works perfectly. Could you possibly explain the syntax quickly? I don't understand how to use the ? or : and I am struggling to google an explanation for them



Yes google would probably not help much here. But fear not it is in the online help (which is far and away the best I have ever seen in a software program). Open help and go to the command help area and it is ~20 items down on the left.

The syntax (which comes in very handy) is the first part is a logical test, in this case matrixB - matrixA < 0 with the following "?". Think of it as making the question. The next part is if the test resolves to true. So in this case if matrixB - matrixA < 0 is true use the value 0. The colon ":" represents an if false result. Or in this case if matrixB - matrixA < 0 is false and the result is greater than 0, the value (matrixB - matrixA) is used.

This test loops over all points so the matrices need to be same size and dimensions and it does a point by point resolution.

Andy
jamesbutler01 wrote:

Thanks! That works perfectly. Could you possibly explain the syntax quickly? I don't understand how to use the ? or : and I am struggling to google an explanation for them


This is a conditional operator with a format like:

<expression> ? <TRUE> : <FALSE>

If expression evaluates true the first alternative is returned and if it evaluates false the second alternative is returned. This can be used with matrices or scalars. However, the elements must be numeric.

Scalar:

Variable nOutput
Variable nTest
sOutput = nTest == 0 ? 0 : 1


If nTest has a value of 0 then sOutput gets a value of 0, otherwise it will get a value of 1.

If you are using waves, particularly with multidimensional waves, then you need to use wave indexing and the test will be applied to all selected points in the wave.
For example...

Make/N=(5, 5) wOutput, wTest
wOutput = wTest[p][q] == 0 ? 0 : wTest[p][q]


The form is general and can be used to do some very clever things.