【发布时间】:2018-02-22 15:36:04
【问题描述】:
我有这个 Matlab 代码:
[arr1 arr2 arr3] = fReadFileBin(filename));
函数体在哪里:
function [Result1 , Result2 , Result3 ] = fReadFileBin(filename)
fid = fopen(filename, 'r');
fseek(fid, 180, 0);
PV = fread(fid, [A*3 B+2], 'float32');
fclose(fid);
Result1 = PV(1:3:3*A, 2:B+1);
Result1 = Result1';
Result2 = PV(2:3:3*A, 2:B+1);
Result2 = Result2';
Result3 = PV(3:3:3*A, 2:B+1);
Result3 = Result3';
结果我有 3 个大小为 BxA 的填充向量并输入 Double。
当我尝试用 C++ 重写它时:
std::vector<std::vector<double>> result;
result.resize(B, std::vector<double>(A));
std::ifstream is(filename, std::ios::binary);
is.seekg(0, std::ios_base::end);
std::size_t size = is.tellg();
is.seekg(0, std::ios_base::beg);
is.seekg( 180, 0);
std::vector<double> PV (size / sizeof(double));
if (!is.read((char*)&PV[0], size))
{
throw std::runtime_error("error reading file");
}
// Load the data
is.read((char*)&PV[0], size);
is.close();
// std::vector<double> Result1 =
// std::vector<double> Result2 =
// std::vector<double> Result3 =
//R=R'
//R[j][i] = R[i][j];
This question 对我来说确实有意义,但仍然不明白如何重写这部分:(1:3:3*A, 2:B+1) in C++?
注意:
-我仅限于使用标准库(没有 boost、mmap 等)。
-我检查了关于冒号的 Mathwork 文档(但仍然无法理解如何实现它)。
【问题讨论】:
-
与其尝试从一种语言逐行翻译到 C++ ,不如从高级术语中考虑该表达式应该做什么。不管它应该做什么,用 C++ 编写等效的代码。
-
1:3:3*A = [1, 1+3, 1+2*3, ...] 直到 3*a; 2:B+1 = [2 B+1] 但是所有这些对我来说没有意义。 (或者我错过了什么)
-
如果你试图写的东西对你没有意义,你就不能用 C++ 写东西。这听起来更像是基于 MatLab 的问题,而不是 C++ 问题。