C语言实现利用reshape函数将float型数组[75*13*13]转换成float型数组[3*25*13*13]
时间: 2023-11-14 16:20:56 浏览: 134
C语言中数组的运算
5星 · 资源好评率100%
代码实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
void reshape(float *src, float *dst, int src_shape[], int dst_shape[]) {
int src_size = 1, dst_size = 1;
for (int i = 0; i < 3; i++) {
src_size *= src_shape[i];
dst_size *= dst_shape[i];
}
if (src_size != dst_size) {
printf("Error: src_size != dst_size");
return;
}
int src_index, dst_index;
for (int i = 0; i < dst_shape[0]; i++) {
for (int j = 0; j < dst_shape[1]; j++) {
for (int k = 0; k < dst_shape[2]; k++) {
dst_index = i * dst_shape[1] * dst_shape[2] + j * dst_shape[2] + k;
src_index = i * src_shape[1] * src_shape[2] * dst_shape[2] + j * src_shape[2] * dst_shape[2] + k * dst_shape[2];
for (int l = 0; l < dst_shape[3]; l++) {
dst[dst_index * dst_shape[3] + l] = src[src_index + l];
}
}
}
}
}
int main() {
float src[75*13*13];
float dst[3*25*13*13];
int src_shape[] = {75, 13, 13};
int dst_shape[] = {3, 25, 13, 13};
// 初始化src数组
for (int i = 0; i < 75*13*13; i++) {
src[i] = i;
}
// 调用reshape函数
reshape(src, dst, src_shape, dst_shape);
// 打印dst数组
for (int i = 0; i < 3*25*13*13; i++) {
printf("%f ", dst[i]);
}
return 0;
}
```
阅读全文