NSConfused... matrices and NSData



Sorry for the long post, but I'm trying to wrap my head around NSData, NSMutableData and matrices.
So I made an example to test my (mis)conception of how this all should work.

Here it is:

- (void) matrixTesting
{
int numRows = 5;
int numCols = 7;
NSMutableData *theMatrix = [NSMutableData dataWithLength:(numRows * numCols * sizeof(float))];
[theMatrix retain];

float *matrix = [theMatrix mutableBytes];

// Set row 3, col 5 to 3.333
matrix[(3 * numRows) + 5] = 3.333;
// Set row 2, col 4 to 6.666
matrix[(2 * numRows) + 4] = 6.666;

int i,j;
for(i=0;i < numRows;i++)
for(j=0;j < numCols;j++)
NSLog(@"Row %i, Col %i = %f", i, j, matrix[i * numRows
+ j]);

int getRow = 3;

NSRange rowRange = {
getRow * numRows, numCols * sizeof(float)
};

float theRow[numCols];
[theMatrix getBytes:theRow range:rowRange];
for(i=0;i < numCols;i++)
NSLog(@"\n Row test --------> Row 3, index %i = %f", i,
theRow[i]);

[theMatrix release];
}

And here's the output:

2009-03-29 09:26:18.339 [1565:813] Row 0, Col 0 = 0.000000
2009-03-29 09:26:18.340 [1565:813] Row 0, Col 1 = 0.000000
2009-03-29 09:26:18.340 [1565:813] Row 0, Col 2 = 0.000000
2009-03-29 09:26:18.341 [1565:813] Row 0, Col 3 = 0.000000
2009-03-29 09:26:18.341 [1565:813] Row 0, Col 4 = 0.000000
2009-03-29 09:26:18.341 [1565:813] Row 0, Col 5 = 0.000000
2009-03-29 09:26:18.342 [1565:813] Row 0, Col 6 = 0.000000
2009-03-29 09:26:18.342 [1565:813] Row 1, Col 0 = 0.000000
2009-03-29 09:26:18.342 [1565:813] Row 1, Col 1 = 0.000000
2009-03-29 09:26:18.342 [1565:813] Row 1, Col 2 = 0.000000
2009-03-29 09:26:18.343 [1565:813] Row 1, Col 3 = 0.000000
2009-03-29 09:26:18.343 [1565:813] Row 1, Col 4 = 0.000000
2009-03-29 09:26:18.343 [1565:813] Row 1, Col 5 = 0.000000
2009-03-29 09:26:18.344 [1565:813] Row 1, Col 6 = 0.000000
2009-03-29 09:26:18.344 [1565:813] Row 2, Col 0 = 0.000000
2009-03-29 09:26:18.344 [1565:813] Row 2, Col 1 = 0.000000
2009-03-29 09:26:18.345 [1565:813] Row 2, Col 2 = 0.000000
2009-03-29 09:26:18.345 [1565:813] Row 2, Col 3 = 0.000000
2009-03-29 09:26:18.345 [1565:813] Row 2, Col 4 = 6.666000 <---- cool!
2009-03-29 09:26:18.346 [1565:813] Row 2, Col 5 = 0.000000
2009-03-29 09:26:18.346 [1565:813] Row 2, Col 6 = 0.000000
2009-03-29 09:26:18.346 [1565:813] Row 3, Col 0 = 0.000000
2009-03-29 09:26:18.347 [1565:813] Row 3, Col 1 = 0.000000
2009-03-29 09:26:18.347 [1565:813] Row 3, Col 2 = 0.000000
2009-03-29 09:26:18.347 [1565:813] Row 3, Col 3 = 0.000000
2009-03-29 09:26:18.348 [1565:813] Row 3, Col 4 = 0.000000
2009-03-29 09:26:18.348 [1565:813] Row 3, Col 5 = 3.333000 <---- also cool!
2009-03-29 09:26:18.348 [1565:813] Row 3, Col 6 = 0.000000
2009-03-29 09:26:18.348 [1565:813] Row 4, Col 0 = 3.333000 <---- huh???
2009-03-29 09:26:18.349 [1565:813] Row 4, Col 1 = 0.000000
2009-03-29 09:26:18.349 [1565:813] Row 4, Col 2 = 0.000000
2009-03-29 09:26:18.349 [1565:813] Row 4, Col 3 = 0.000000
2009-03-29 09:26:18.350 [1565:813] Row 4, Col 4 = 0.000000
2009-03-29 09:26:18.350 [1565:813] Row 4, Col 5 = 0.000000
2009-03-29 09:26:18.350 [1565:813] Row 4, Col 6 = 0.000000
2009-03-29 09:26:18.351 [1565:813] Row test --------> Row 3, index 0 = 0.000000
2009-03-29 09:26:18.351 [1565:813] Row test --------> Row 3, index 1 = 0.000000
2009-03-29 09:26:18.351 [1565:813] Row test --------> Row 3, index 2 = 0.000000
2009-03-29 09:26:18.352 [1565:813] Row test --------> Row 3, index 3 = 0.000000
2009-03-29 09:26:18.352 [1565:813] Row test --------> Row 3, index 4 = 0.000000
2009-03-29 09:26:18.352 [1565:813] Row test --------> Row 3, index 5 = 0.000000 <--- yikes!
2009-03-29 09:26:18.353 [1565:813] Row test --------> Row 3, index 6 = 0.000000

So why would it set row 4, col 0 to 3.333? And why didn't the range I pulled out for row 3 contain the 3.333?
Obviously I'm not understanding a couple of things here, so any help very much appreciated!


J.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy