'reshape' function in MEX ?

1 回表示 (過去 30 日間)
Krishna
Krishna 2013 年 10 月 8 日
編集済み: James Tursa 2024 年 2 月 28 日
I can reshape a 4*4 matrix to '2*8' or '1*16' matrix using 'reshape' function from the command window. I want to achieve the same in the MEX file and this is not working in the code. Can someone please tell me how I can achieve for my code or how to use this 'reshape' function. Thanks in advance.
  1 件のコメント
Jan
Jan 2013 年 10 月 9 日
編集済み: Jan 2013 年 10 月 9 日
If you show us the code, we can fix it. Without seeing the code, suggestion must be based on bold guessing.
"Is not working" does not explain what exactly happens. Do you can a compiler error or does the function crash or does it leave the dimensions untouched?
Please explain, if you write the MEX function in C, C++ or Fortran.

サインインしてコメントする。

回答 (2 件)

Honglei Chen
Honglei Chen 2013 年 10 月 8 日
Are you getting an error from MATLAB Coder when compiling to MEX? If so, it is probably because you assign the result of reshape to the original variable so the variable becomes variable size and MATLAB Coder cannot deal with it. Instead of using
A = reshape(A,2,8)
You may want to use
B = reshape(A,2,8)
instead.
  2 件のコメント
Krishna
Krishna 2013 年 10 月 9 日
No, its not such an error. It's like I get some linking error caused due to 'reshape' function.
James Tursa
James Tursa 2013 年 10 月 9 日
Is this code that you created or did the Coder create it? Please show the code that is doing the reshape.

サインインしてコメントする。


Christopher Creutzig
Christopher Creutzig 2024 年 2 月 12 日
編集済み: Christopher Creutzig 2024 年 2 月 12 日
Just because this question seems to still get a number of views: The mex function equivalent to reshape is called mxSetDimensions.
  2 件のコメント
James Tursa
James Tursa 2024 年 2 月 12 日
編集済み: James Tursa 2024 年 2 月 28 日
Some comments about this answer:
1) mxSetDimensions should only be used on mxArrays created inside the mex function. It should not be used on any of the prhs[ ] inputs as that can create unintended side effects if the input arguments are ever passed in by reference or reference copy. If you need to treat a prhs[ ] input as having different dimensions inside a mex routine, you could always just create your own dimension array off to the side and use that in your code instead of what is obtained from the mxGetDimensions routine.
2) mxSetDimensions should not be used on sparse matrices, since that would create a disconnect between the shape of the matrix and the Ir and Jc data (row & column indexing) inside the mxArray. This will either lead to incorrect results or a MATLAB crash downstream in your code. There are several steps involved in correctly reshaping a sparse matrix that involve the row & column indexing.
Christopher Creutzig
Christopher Creutzig 2024 年 2 月 13 日
Agreed, modifying prhs[] data is a bad idea, and sparse matrices are always special. I have sent a suggestion to the documentation team to call both these things out on the page.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by