C语言实现自定义reshape函数将数组[3*25*13*13]转成[3*13*13*25]
时间: 2023-11-14 07:03:41 浏览: 124
```
#include <stdio.h>
#include <stdlib.h>
void reshape(float* input, float* output, int* shape) {
int size = shape[0] * shape[1] * shape[2] * shape[3];
int input_step = shape[1] * shape[2] * shape[3];
int output_step = shape[2] * shape[3];
for (int i = 0; i < size; i++) {
int in_idx = i;
int out_idx = (i % output_step) * shape[1] * shape[0]
+ (i / output_step % shape[3]) * shape[0]
+ (i / input_step % shape[2])
+ (i / (input_step * shape[2]) % shape[1]) * shape[2];
output[out_idx] = input[in_idx];
}
}
int main() {
float input[3*25*13*13];
float output[3*13*13*25];
int shape[] = {3, 25, 13, 13};
// initialize input
for (int i = 0; i < 3*25*13*13; i++) {
input[i] = i;
}
// reshape
reshape(input, output, shape);
// print output
for (int i = 0; i < 3*13*13*25; i++) {
printf("%f ", output[i]);
}
return 0;
}
```
阅读全文