我已使用MLMultiArray
初始化initWithDataPointer
,如下面的代码所示:
float count = 512 * 384;
double *tempBuffer = malloc(count * sizeof(double));
NSError *error = NULL;
NSArray *shape = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:512],[NSNumber numberWithInt:384], nil];
NSArray *stride = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:1], nil];
MLMultiArray *mlMultiArray = [[MLMultiArray alloc] initWithDataPointer:tempBuffer
shape:shape
dataType:MLMultiArrayDataTypeDouble
strides:stride
deallocator:^(void * _Nonnull bytes) { free(bytes); }
error:&error];
根据此link中提到的MLMultiArray
文档,subscript
需要用于访问元素。
如果我按照显示的方式访问元素,它是否正确?
NSNumber *val = [mlMultiArray objectForKeyedSubscript:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:1], nil]];
答案 0 :(得分:5)
我建议您使用mlMultiArray.dataPointer
,将其强制转换为double *
,然后直接访问数据缓冲区的内容。您可以计算元素i, j, k
使用步幅的位置:
double *ptr = (double *) mlMultiArray.dataPointer;
NSInteger offset = i*stride[0].intValue + j*stride[1].intValue + k*stride[2].intValue;
double val = ptr[offset];