Matlab to C++: Accessing a 1228x681 Matrix

  • MATLAB
  • Thread starter schabbir
  • Start date
  • Tags
    C++ Matlab
In summary, the person is asking for help with accessing a mat file in C++. They are told that C++ does not have a built-in concept of matrices and they will need to build it using arrays or a library. They are also given a link to a website and documentation on how to save the file in ASCII format and read it in using cin << operations.
  • #1
schabbir
2
0
Hi,

I have a mat file, which is a 1228 x 681 matrix and I want to access this file in c++. Can anyone help me how to do this?
 
Physics news on Phys.org
  • #2
C++ has no notion of matrices. Unlike other languages C++ starts you out with practically nothing, but it does give you the unprecedented ability to create whatever structures you want with practically any syntax you want. Thus you can easily get matrices that behave any way you like, but you will need to build this upon the concept of arrays -- either explicitly, or implicitly by using std::vector, or someone else's matrix library.
 
  • #3
  • #4
You can save it in ASCII format instead, and then read it in using a cin << operation. Again, as junglebeast notes, C++ has no notion of matrices, so you can either be content with reading stuff into a long 1-D array, parsing it into a 2-D array using some loops, or possibly using an STL container class (I vaguely recall doing matrix transforms with overloaded functions years ago in my C++ class in undergrad--unfortunately, you needed to write your own operators for matrix multiplication, transposition, addition, etc.):
http://en.wikipedia.org/wiki/Standard_Template_Library

MATLAB Save documentation (look about half-way down the page, under the heading MAT-File Format Options):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.html
 

1. How can I convert a Matlab matrix to C++?

There are several ways to convert a Matlab matrix to C++. One option is to use the Matlab Coder tool, which can automatically generate C++ code from your Matlab script. Another option is to manually write C++ code that mimics the functionality of your Matlab script. You can also use a third-party library such as Armadillo or Eigen to handle matrix operations in C++.

2. How do I access a specific element in a 1228x681 matrix in C++?

In C++, matrices are typically stored in row-major order, meaning that the elements in a row are stored contiguously in memory. To access a specific element in a 1228x681 matrix, you can use the formula index = row * numColumns + column, where "row" and "column" are the indices of the element you want to access and "numColumns" is the number of columns in the matrix.

3. Can I use the same indexing syntax for Matlab and C++ matrices?

No, Matlab and C++ use different indexing syntax for matrices. In Matlab, the first index indicates the row and the second index indicates the column. In C++, the first index indicates the row, but the second index indicates the column multiplied by the number of columns in the matrix. For example, to access the element in the third row and fourth column of a 1228x681 matrix, you would use the syntax M(3,4) in Matlab and M[3 * 681 + 4] in C++.

4. How do I perform matrix operations in C++?

In C++, you can use loops and built-in functions to perform matrix operations. However, this can be tedious and error-prone. As mentioned earlier, you can also use third-party libraries such as Armadillo or Eigen, which provide efficient and easy-to-use functions for matrix operations in C++.

5. Is it possible to convert a C++ matrix back to Matlab?

Yes, it is possible to convert a C++ matrix back to Matlab. You can use the mxCreateNumericMatrix function from the Matlab C API to create a new Matlab matrix and then use memcpy to copy the data from your C++ matrix into the Matlab matrix. However, this process can be complicated and it is recommended to use the Matlab Coder tool if you need to convert between Matlab and C++ matrices frequently.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
963
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top