Error: Matrix dimensions must agree & Error: Function called failed while converting my code to C

1 view (last 30 days)
Hi, I am trying to convert this code to C (this is the part of the code that brings problems):
z = complex(zeros(size(x)));
z = 1./(1i.*x);
N = length(x);
mat = complex(zeros(N,n+2));
mat = (z).^(0:n+1);
But I keep getting those 2 errors (matrix dimensions must agree & funcion called failed) whenever I do:
mat = (z).^(0:n+1); or
mat = power(z,0:(n+1));
Both "n" and "x" are inputs to my function, where n is an integer scalar and x is a real vector or real scalar (already did the validation for that).
The problem arises when "x" is a (column) vector (if it is a scalar, it works just fine. I handle row vectors separately - I make the column vectors - lets please focus on column vectors).
It is worth noting that there are no probles whatsoever when I call the function from a Matlab script or a Matlab function. It works like a charm. The problems arise when trying to "externalize this function"/ "make this function extrinsic"/"make this function C compilable"/etc.
One last curious thing is that when using basically the same lines of code in other function, no problems arise. The essential difference between this two functions is the following line:
z = 1./(1i.*x);
One functions uses the input "x" while the other uses "z", real and complex (column) vectors respectively.
I really hope someone out there can help me. I appreciate and thank your help in advance.
PS: English is not my native language. Hope I did'nt make any mistakes.

Accepted Answer

James Tursa
James Tursa on 22 May 2020
This line:
mat = (z).^(0:n+1);
uses implicit expansion. z is a column vector and (0:n+1) is a row vector, so the result is intended to be a matrix.
Maybe that is the issue. How are you converting this to C code?
  3 Comments
James Tursa
James Tursa on 22 May 2020
Maybe if you avoid the implicit expansion it will work? E.g., in earlier versions of MATLAB you would call bsxfun for this:
mat = bsxfun(@power,z,0:n+1);
Ayrton Urviola
Ayrton Urviola on 22 May 2020
I don't know why, nor how... but it works!!! THANK YOU!! I was almost getting a headache because of this... I've been at this almost 4h straight... I am very grateful...

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!