mwArray 定義矩陣變量

 

轉載:https://blog.csdn.net/qq_26376985/article/details/50203021

<1>mwArray 定義矩陣變量

  mwArray  A(rows, cols, type)

參數說明:

   A       : 變量名

   rows:行數

   col   :列數

   type :數t據類型

 

mwArray 

 

  mwArray :C++用它向MATLAB傳遞輸 i/o 參數。MATLAB中的參數都是矩陣表示,哪怕是1*1的矩陣。

 1)構造函數

  mwArray(mwSize num_dims, const mwSize* dims, mxClassID mxID,mxComplexity cmplx = mxREAL)

  num_rows :行數; num_cols :列數;mxID:The data type type of the matrix.: cmplx:The complexity of the matrix (numeric types only).

mwArray a(2, 2, mxDOUBLE_CLASS);

mwArray b(3, 3, mxSINGLE_CLASS, mxCOMPLEX);

mwArray c(2, 3, mxCELL_CLASS);

mwArray(mwSize num_rows, mwSize num_cols, mxClassID mxID,mxComplexity cmplx = mxREAL)

mwArray(const char* str)

mwArray(mwSize num_strings, const char** str)

mwArray(mwSize num_rows, mwSize num_cols, int num_fields,const char** fieldnames)

mwArray(mwSize num_dims, const mwSize* dims, int num_fields,const char** fieldnames)

mwArray(const mwArray& arr)

mwArray( re)

mwArray( re, im)

2) 方法

 

mwArray SharedCopy() const

mwArray Serialize() const

mxClassID ClassID() const

int ElementSize() const

size_t ElementSize() const

mwSize NumberOfElements() const

mwSize NumberOfNonZeros() const

mwSize MaximumNonZeros() const

mwSize NumberOfDimensions() const

int NumberOfFields() const

mwString GetFieldName(int index)

mwArray GetDimensions() const

bool IsEmpty() const

bool IsSparse() const

bool IsNumeric() const

bool IsComplex() const

bool Equals(const mwArray& arr) const

int CompareTo(const mwArray& arr) const

int HashCode() const

mwString ToString() const

mwArray RowIndex() const

mwArray ColumnIndex() const

void MakeComplex()

mwArray Get(mwSize num_indices, ...)

mwArray Get(const char* name, mwSize num_indices, ...)

mwArray Get(mwSize num_indices, const mwIndex* index)

mwArray Get(const char* name, mwSize num_indices, const mwIndex*index)

      #include "mclcppclass.h"

      double data[4] = {1.0, 2.0, 3.0, 4.0};

      double x;

       mwArray a(2, 2, mxDOUBLE_CLASS);

      a.SetData(data, 4);

      x = a.Get(1,1); // x = 1.0

       x = a.Get(2, 1, 2); // x = 3.0,忽略第一個數,二三位代表第一行第二列?

       x = a.Get(2, 2, 2); // x = 4.0,忽略第一個數,二三位代表第二行第二列?

mwArray Get(mwSize num_indices,....)根據索引返回陣列元素,其中num_indices表示索引數目。Get函數中輸入的索引均從1起始。例如

double data[4]={1.0,2.0,3.0,4.0};

mwArray a(2,2,mxDOUBLE_CLASS);

double x;

a.SetData(data,4);

x=a.Get(2,2,2);//返回4 

x=a.Get(1,3);//返回3

mwArray Get(const char *name, mwSize num_indices,...) 返回結構體域名爲name,指定索引的結構體域,其中num_indices表示索引的數目。Get函數中輸入的索引均從1起始。例如

const char* fields[]={"a","b","c"};

mwArray a(1,1,3,fields); //b=a(1).a; 

mwArray b=a.Get("a",1,1);//b=a(1,1).b;

mwArray b=a.Get("b",2,1,1);

 

mwArray Real()

mwArray Imag()

void Set(const mwArray& arr)

void GetData(* buffer, mwSize len) const

void GetLogicalData(mxLogical* buffer, mwSize len) const

void GetCharData(mxChar* buffer, mwSize len) const

void SetData(* buffer, mwSize len)

#include "mclcppclass.h"

double rdata[4] = {1.0, 2.0, 3.0, 4.0};

double data_copy[4] ;

mwArray a(2, 2, mxDOUBLE_CLASS);

a.SetData(rdata, 4);

a.GetData(data_copy, 4);  // 取出a中的數據。

void SetLogicalData(mxLogical* buffer, mwSize len)

void SetCharData(mxChar* buffer, mwSize len)

4.  mwArray 與 mxArray的用法區別:

Seventy
http://www.simwe.com/forum/post/view?bid=19&id=361497&sty=1&tpg=7&age=100

首先,mxArray是Matlab C 函數庫的結構體,而mwArray是Matlab C++ 函數庫中對mxArray的包裝類。
其次,二者的內存管理方式不同。mxArray的內存管理方式比較鬆散,由於是C函數庫,沒有數據封裝,必須對臨時陣列和約束陣列的概念極爲明確,並且須小心地防止內存泄漏(要多寫好多代碼)。儘管有自動內存管理機制(mlfEnterNewContext,mlfReleasePreviousContext),仍然要處處調用mlfAssign,麻煩得很。然而mwArray就好的多,一切交給C++對象去搞定,你只要放心地用就可以了。不過Matlab C++函數庫爲了防止頻繁內存分配和釋放重寫了內存分配和釋放等函數。你會發現對於mwArray a,b; a=b;實際上並沒有生成兩個相同數據塊,只是指針,只有發成數據改變時才copy完整數據。
再次,這兩個東西各自有一套與之相對應的函數。函數的返回值類型不同,需要多加註意。
最後,用的時候mxArray要使用指針,而mwArray直接當類對象使。如果你不是Hardcore級的牛人或具有Hardcore傾向的潛牛人,我強烈建議你使用後者!

舉一個簡單的例子:如果你需要計算c=a+b那麼兩者的區別是這樣的:

mxArray:
void fun(){
……
double d_a=1,d_b=2;
mxArray *a,*b,*c;
mlfEnterNewContext(0,0);
mlfAssign(&a,mlfScalar(d_a));
mlfAssign(&b,mlfScalar(d_b));
mlfAssign(&c,mlfPlus(a,b));
……
mlfReleasePreviousContext(0,0);
mxDestroyArray(a);
mxDestroyArray(b);
mxDestroyArray(c);
}

mwArray:
void fun(){
……
mwArray a,b,c;
a=1;
b=2;
c=a+b;
……

}

 

本文主要介紹:matlab與C++結合的數據類型mwArray的一些基本知識,以及在C++中,如何對mwArray賦值,主要包括矩陣賦值和字符串賦值。

首先,說明mwArray數據類型不同於mxArray,所以許多關於mxArray的用法,對mwArray不一定適用,具體區別見博文:

http://www.cnblogs.com/kmliang/archive/2012/08/29/2662943.html

一.矩陣賦值

<1>mwArray 定義矩陣變量

  mwArray  A(rows, cols, type)

參數說明:

   A       : 變量名

   rows:行數

   col   :列數

   type :數t據類型

type類型有:

 

[cpp] view plain copy

  1. typedef enum  
  2. {  
  3.     mxUNKNOWN_CLASS = 0, //未知類型  
  4.     mxCELL_CLASS, //細胞類型  
  5.     mxSTRUCT_CLASS, //結構類型  
  6.     mxLOGICAL_CLASS, //布爾類型  
  7.     mxCHAR_CLASS,  //字符串類型  
  8.     mxVOID_CLASS,  //void類型  
  9.     mxDOUBLE_CLASS,   
  10.     mxSINGLE_CLASS, //單精度浮點數  
  11.     mxINT8_CLASS, //  
  12.     mxUINT8_CLASS,  
  13.     mxINT16_CLASS,  
  14.     mxUINT16_CLASS,  
  15.     mxINT32_CLASS,  
  16.     mxUINT32_CLASS,  
  17.     mxINT64_CLASS,  
  18.     mxUINT64_CLASS,  
  19.     mxFUNCTION_CLASS, //函數類型  
  20.     mxOPAQUE_CLASS, //  
  21.     mxOBJECT_CLASS  //對象類型  
  22. }  

[cpp] view plain copy

  1. typedef enum  
  2. {  
  3.     mxUNKNOWN_CLASS = 0, //未知類型  
  4.     mxCELL_CLASS, //細胞類型  
  5.     mxSTRUCT_CLASS, //結構類型  
  6.     mxLOGICAL_CLASS, //布爾類型  
  7.     mxCHAR_CLASS,  //字符串類型  
  8.     mxVOID_CLASS,  //void類型  
  9.     mxDOUBLE_CLASS,   
  10.     mxSINGLE_CLASS, //單精度浮點數  
  11.     mxINT8_CLASS, //  
  12.     mxUINT8_CLASS,  
  13.     mxINT16_CLASS,  
  14.     mxUINT16_CLASS,  
  15.     mxINT32_CLASS,  
  16.     mxUINT32_CLASS,  
  17.     mxINT64_CLASS,  
  18.     mxUINT64_CLASS,  
  19.     mxFUNCTION_CLASS, //函數類型  
  20.     mxOPAQUE_CLASS, //  
  21.     mxOBJECT_CLASS  //對象類型  
  22. }  


整體含義是:定義矩陣A,行數爲:rows,列數爲:cols,類型爲:type

 

注: 如果參數不是矩陣,只是一個數,令 rows=1,cols=1即可。

<2>矩陣賦初值:

 

[cpp] view plain copy

  1. int a[6] = {1,2,3,4,5,6}  
  2. mwArray A(2,3,mxINT32_CLASS);    
  3. A.SetData(a,6); //第二個參數爲要設置的數的個數,大小可設爲rows*cols  

[cpp] view plain copy

  1. int a[6] = {1,2,3,4,5,6}  
  2. mwArray A(2,3,mxINT32_CLASS);    
  3. A.SetData(a,6); //第二個參數爲要設置的數的個數,大小可設爲rows*cols  


注:該過程相當於把1*6的矩陣,轉化爲2*3的矩陣,matlab轉化順序是,先排第一列,由上到下爲a[0] a[1],然後排第二列,由上到下爲a[2] a[3],即轉化後的A爲:

 

1     3      5

2     4      6

如果要使A爲:

1      2     3

4      5     6

需這樣賦值:

 

[cpp] view plain copy

  1. <pre name="code" class="cpp">int a[6] = {1,2,3,4,5,6}  
  2. mwArray A(3,2,mxINT32_CLASS);  //修改此處:行列數互換  
  3. A.SetData(a,6); //第二個參數爲要設置的數的個數,大小可設爲rows*cols  

[cpp] view plain copy

  1. <pre name="code" class="cpp">int a[6] = {1,2,3,4,5,6}  
  2. mwArray A(3,2,mxINT32_CLASS);  //修改此處:行列數互換  
  3. A.SetData(a,6); //第二個參數爲要設置的數的個數,大小可設爲rows*cols  

 

 

 

 

 


 

此時生成的A爲:

 

1      4

2      5

3      6

該矩陣轉置之後,既可以達到所需形式,轉置過程可以在matlab的.m文件中添加,先修改.m,然後在生成dll、lib、h文件。

尤其是,在圖像處理時,如果傳遞的矩陣爲圖像數據矩陣,要採用後一種方法賦值,否則,圖像會嚴重變形失真。二、字符串賦值

 

[cpp] view plain copy

  1. char str[5] = "abcd";  
  2. //或 CString str = "abcd"  
  3. mwArray mwA(str); 

 

發佈了10 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章