Matrix and Error 10: Array subscript out of range

Hello,
I know this is common error so I hope I won't get slapped on the fingers. I read several content about this but can't understand the problem specifically as I'm dealing with matrixes and not arrays (this is in the CBT):

_TRACEF( "Barcount: %g", BarCount ); // 66889

tradesadded = 0;
totalTrades= 446;
totalFilteredTrades = 128;

filteredTradesMatrixWithNullValues = Matrix( totalTrades, 2 ); // this was filled elsewhere but I put it here for practical purposes
filteredTradesMatrix = Matrix( totalFilteredTrades, 2 );

    if( BarCount > totaltrades ) 
    {
        for( i = 0; i < totaltrades; i++ )
        {
            if( IsNull( filteredTradesMatrixWithNullValues[ i ][ 0 ] ) )
            {
                continue;
            }
            else
            {
                _TRACE( "Filtered trade: " + filteredTradesMatrixWithNullValues[ i ][ 0 ] );
                filteredTradesMatrix[ i ][ 0 ] = filteredTradesMatrixWithNullValues[ i ][ 0 ];
                tradesadded++;
                _TRACE( "tradesadded: " + tradesadded );
  
            }

        }
    }

This stops at after 45 values have been stuffed in filteredTradesMatrix as you can see on image.

itstops

Is the problem because I try to mix different sized matrixes?
If so then why stop after 45 values have been put inside filteredTradesMatrix, where there is room for 128?
This code is supposed to add no more than "totalFilteredTrades" (128) rows into filteredTradesMatrix.

You iterate from 0 to totaltrades-1 (0 to 446-1) but filteredTradesMatrix (rownum 128) has smaller size than matrix filteredTradesMatrixWithNullValues (rownum 446). 128 is less than 446. That is why you get subscript out of range (Error 10) for filteredTradesMatrix. As for 45... I guess you had totalTrades = 46 (so it ends at index 45 (46-1)) before and then you changed to 446.

1 Like

Thank you for your answer, now I see the problem I must take into consideration the largest iteration, and not how much "hit" I got in the "if" statement. I wrongly assumed that iteration that didn't put value inside the smaller matrix were "skipped".

So.... is there any solution for mixing different sized Matrixes, so some selected values from the largest ones, end up into the smaller one?

Your code does not make sense to me and what you want to achieve.

But to prevent error 10 you may use Min() function

rows = Min(totalFilteredTrades, totaltrades);

for( i = 0; i < rows; i++ ) {

}

or

for ( i = 0; i <  totaltrades; i++ ) {

      if ( i < totalFilteredTrades ) {

      }
}

etc....


AFAIU, you have several NULL there in filteredTradesMatrixWithNullValues and you want to have a block without Null(?) Yes/No?

So either

unfiltered = MxFromString("[Null;2;1;Null;6;4;Null]");
MxToString(unfiltered);

rownum1 = MxGetSize(unfiltered, 0);
for( i = 0, n = 0; i < rownum1; i++ ) {
	if ( ! IsNull(unfiltered[i][0]) )
		n++;
}
printf( "\nrows: %g\n\n", n );

filtered = Matrix( n, 1);
for ( i = 0, n = 0; i < rownum1; i++ ) {
	if( ! IsNull(unfiltered[i][0]) ) {
		filtered[n][0] = unfiltered[i][0];
		n++;
	}
}

printf( MxToString(filtered) );

6

Or using sorting function

unfiltered = MxFromString("[Null;2;1;Null;6;4;Null]");
MxToString(unfiltered);

rownum1 = MxGetSize(unfiltered, 0);
for( i = 0, n = 0; i < rownum1; i++ ) {
	if ( ! IsNull(unfiltered[i][0]) )
		n++;
}
printf( "\nrows: %g\n\n", n );

filtered = MxSortRows(unfiltered, True, 0);
filtered = MxGetBlock(filtered, Max(0, n-1), rownum1-1, 0, MxGetSize(filtered, 1)-1);
printf( MxToString(filtered) );

// sorting back to original position (if there is additional indexing column)

7

1 Like

Thanks for solutions, I was able to implement what I wanted.
Basically I wanted a list of filtered trades from the original sequence, to run custom MonteCarlo stats on it. So I needed a Matrix with only those filtered trades, to shuffle, them, then compute some stats.....