如何比较C ++和MATLAB之间的结果? [英] How to compare results between C++ and MATLAB?

查看:145
本文介绍了如何比较C ++和MATLAB之间的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以在多个阶段处理2D数组.我想将其结果与MATLAB进行比较.我也许可以使用std :: cout在控制台上处理2D数组之前(在块上起作用),然后在2D数组上打印一些8x8大小的块,然后将这些数字手动键入到MATLAB中.但这是容易出错和冗长的事情.有没有一种方法可以方便地将这些数据输入到MATLAB中?

I have a program that processes 2D array of doubles at several stages. I want to compare its results with MATLAB. I can perhaps use std::cout to print some of 8x8 size blocks the 2D array before and after processing (the algorithm works on blocks) onto the console and then manually type those numbers into MATLAB. But that is error prone and tedius. Is there a way to conviniently get this data into MATLAB?

Matlab内置的功能可以简化许多事情.我想将在C ++程序中处理之前和之后的数据获取到MATLAB中,然后对其进行一些检查,例如绘制图形和东西.如何将C ++程序中的数据获取到MATLAB中?

Matlab has built in functions to simplify many things. I want to get the data before and after processing in the C++ program into MATLAB and then run some checks on it e.g draw graphs and stuff. How do I get the data from the C++ program into MATLAB?

推荐答案

您是否考虑过使用mex函数?如果您确实使用它们,我强烈建议您使用 Armadillo 库,该库提供了方便的数据类型和在MATLAB和C ++之间切换的方法.

Have you considered using mex functions? If you do use them, I highly recommend using the Armadillo library, which provides convenient data types and methods to switch between MATLAB and C++.

例如

#include "mex.h"
#include <armadillo>
#include "armaMex.hpp"
#include <sstream>    

// gateway function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{     
// get data from MATLAB
mat X = conv_to<mat>::from(armaGetPr(prhs[0],true));
mat Y = conv_to<mat>::from(armaGetPr(prhs[1],true));

// do some stuff
mat Z = X + Y;

// Print data in matlab
std::ostringstream buffer;
buffer << "X = " << X << endl;
buffer << "Y = " << Y << endl;
buffer << "Z = " << Z << endl;
mexPrintf("%s", buffer.str().c_str());    

// send data back to Matlab
plhs[0] = armaCreateMxMatrix(Z.n_rows, Z.n_cols, mxDOUBLE_CLASS, mxREAL); 
armaSetPr(plhs[0], conv_to<mat>::from(Z));

return;
}

这篇关于如何比较C ++和MATLAB之间的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆