mwArray的get函數用法

mwArray的get函數用法

1、應用:
在matlab與C++混合編程中:
使用get函數將mwArray類型的數組中的數據存入vector<vector<double>> v中:

mwArray a(col, row, mxDOUBLE_CLASS, mxREAL);//列優先,先列後行: 構建一個 col列 X row行 的matlab矩陣
a.Get(1, index);// 按matlab存儲方式(列優先存儲,首元素下標爲1)取第index個元素
a.Get(index, index);//等同於a.Get(1, index); 第一個參數不知道是什麼意思...
a.Get(1, col, row);// a.Get(未知,列,行)

2、代碼如下:

#include <iostream>
#include "mclmcr.h"
#include "matrix.h"
#include "mclcppclass.h"
#include<vector>
using namespace std;

int main(){
	double data[] = { 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112 };
	int len = sizeof(data) / sizeof(data[0]);
	int col = 3;
	int row = len / col;

	mwArray a(col, row, mxDOUBLE_CLASS, mxREAL);//列優先,先列後行: 構建一個 col列 X row行 的matlab矩陣

	a.SetData(data, len);
	
	vector<vector<double>> v;
	v.resize(row);
	for (int i = 0; i < row; i++){
		for (int j = 1; j <= col; j++){
			v[i].push_back(a.Get(1, i*col + j));
	//		v[i].push_back(a.Get(i*col + j, i*col + j));//上下兩行的效果一致!!不知道第一個參數是什麼意思??
		}
	}

	for (auto row : v){
		for (auto col : row){
			cout << col << "\t";
		}
		cout << endl;
	}
	cout << endl;

	vector<vector<double>> v1;
	v1.resize(row);
	for (int i = 0; i < row; i++){
		for (int j = 1; j <= col; j++){// j表示:a的第j列元素
			v1[i].push_back(a.Get(1, j, i + 1));// a.Get(未知,列,行)
		}
	}

	for (auto row : v1){
		for (auto col : row){
			cout << col << "\t";
		}
		cout << endl;
	}
	cout << endl;

	double x;
	x = a.Get(3, 2, 4); // x = 111
	cout << x << endl;

	x = a.Get(1, 1, 4); // x = 110
	cout << x << endl;

	x = a.Get(1, 3, 4); // x = 112 // a.Get(未知,列,行)
	cout << x << endl;
	
	return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章