Info

この質問は閉じられています。 編集または回答するには再度開いてください。

A function works in MATLAB but shows an error in C#

1 回表示 (過去 30 日間)
Mehdi
Mehdi 2015 年 8 月 10 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello all,
I have a function which is working very well in MATALAB. But interestingly when I run it in C# I get this error:
Index exceeds matrix dimensions.
C# code:
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using A;
double[] Bx = new double[N];
double[,] PP = new double[N,N];
double[,] W = new double[N,N];
// Some calculation for Bx , PP & W
MWNumericArray arr1 = PP;
MWNumericArray arr2 = Bx;
MWNumericArray arr3 = W;
ACLASS OBJ = new ACLASS();
MWArray RESULT = (MWNumericArray)OBJ.A((MWNumericArray)arr1, (MWNumericArray)arr2, (MWNumericArray)arr3,3, 2,1,1);
// Copy back the RESULT in PP
PP = (double[,])((MWNumericArray)RESULT).ToArray(MWArrayComponent.Real);
  • What does happen in C# which disturbs matrix dimension ?
  • Could it be because of difference arrays in C# start from 0 and in MATLAB start from 1 ?
Any help ?
Thanks

回答 (1 件)

Varun Bhaskar
Varun Bhaskar 2015 年 8 月 12 日
Hello,
Please follow the simple example of the 'magic square' code integration in the following link :
Since you have already declared arr1, arr2, and arr3 you do not need to typecast again in the next line of code. Also, as you said C# array indices start from 0, so MWNumeric Array indices start from 0 to n-1, n being the size of the array.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by