Check 1D wave for non-zero values and count them

Hi all,

Could you please tell me, how to check rows of matrix for non-zero values? For example, 0th row consists only of zeros, 1st row also consists only of zeros, 2nd row constists of zeros and non-zeros, and so on. How to implement the following:

for (i=0; i<512; i+=1)

    if (i-th row consists only of zeros)

        continue

    elseif (i-th row contains non-zero values)

        count number of non-zero values in this row

endfor

In reply to by johnweeks

johnweeks wrote:

Seems like MatrixOP sumRows would result in a zero for any row that is all zeroes.

and any other row that sums to zero...

if there is a possibility of that happening, sum(abs()) is more robust

For integer waves you could use:

MatrixOP/O aa=equal(equal(ddd,0),numCols(ddd))

for floating point waves you could use some tolerance:

Variable tol=1e-15

MatrixOP/O aa=equal(greater(tol,abs(ddd)),numCols(ddd))

 

A.G.

I have an image that is (512,512) matrix, and wave scaling (in millimeters) is applied to this matrix along X and Y axes.

How to extract i-th row from scaled 2D wave?

For example, if I try to extract 2nd row:

print image1[2][]

it doesn't work (Igor says: Syntax Error - expected operand).

But if I write:

print image1[2][510]

i.e. specifying the column number, Igor prints the corresponding value. But I need to extract the entire row.

In reply to by Igor

Igor wrote:

Try:

 

MatrixOP/O myRow=row(image1,rowNumber)

Thank you!

Another variant that I found on this forum (https://www.wavemetrics.com/forum/general/extracting-data-2d-wave) is:

Duplicate/O/RMD=[2][] image1,tmp

This variant preserves scaling.

I also have a question about 1D wave filling. Is it possible to create empty wave and then fill it with numbers on every step in loop (i.e. append numbers to the list like in Python)? I need empty wave because I don't know in advance what size of wave is necessary.

Please note that both variants produce a 2D wave that contains 1 row by N columns.  If this is a problem you can add ^t to the MatrixOP command or use Redimension in the other approach.

WAVE wave0
Variable npnts = numpnts(wave0)
Variable i
for (i = 0; i < npnts; i++)
   wave0[i] = sqrt(i)
endfor

This particular use of the loop is not efficient; anytime you want to fill a wave with values it is more efficient to use a wave assignment statement like this:

wave0=sqrt(p)

But my loop shows you how to put values into each point of a wave inside a loop.

Be sure to read about wave assignments: DisplayHelpTopic "Waveform Arithmetic and Assignment"

In reply to by johnweeks

johnweeks wrote:

WAVE wave0
Variable npnts = numpnts(wave0)
Variable i
for (i = 0; i < npnts; i++)
   wave0[i] = sqrt(i)
endfor

This particular use of the loop is not efficient; anytime you want to fill a wave with values it is more efficient to use a wave assignment statement like this:

wave0=sqrt(p)

But my loop shows you how to put values into each point of a wave inside a loop.

Be sure to read about wave assignments: DisplayHelpTopic "Waveform Arithmetic and Assignment"

Probably you misunderstood me. I meant that I'd like to create wave without any elements. And then, in for-loop, depending on fulfilling if-conditions, I'd like to fill the wave step by step. I tried to use

make/O/D/N=0 test1
test1[0]=10

The first command creates empty wave, but I can't work with this wave further - second command doesn't work, IgorPro says: "Command Error, the wave has zero data allocated". So it appears that it is mandatory to allocate space for the wave. But what if I want to create totally empty wave and then gradually fill it with numbers? Is it possible? I've read "Waveform Arithmetic and Assignment" but I didn't find answer.

Please be aware that inserting points one at a time is quite inefficient, as it requires re-allocating the memory for the wave each time. It is better if you can figure out the number of points you need ahead of time. I realize that it can be quite difficult to do that, though.

If you only need a few points to a few hundred points, the inefficiency may not be important.

To follow up on John's note about adding points ... you could create a wave that has the maximum number of points in it that you think you will ever need, set it completely to NaN, and then fill sequentially with 1 or 0 as needed.

In reply to by jjweimer

jjweimer wrote:

To follow up on John's note about adding points ... you could create a wave that has the maximum number of points in it that you think you will ever need, set it completely to NaN, and then fill sequentially with 1 or 0 as needed.

Thank you!