jonathf / matlab2cpp

Convertion program from Matlab to C++ using Armadillo
BSD 3-Clause "New" or "Revised" License
181 stars 64 forks source link

vectorise vs vectorize bug #128

Closed taiwuchiang closed 7 years ago

taiwuchiang commented 7 years ago

Matlab code:

a = rand(5,5); b = sum(a(:));

Generated C++ code: int main(int argc, char** argv) { double b ; mat a ; a = arma::randu(5, 5) ; b = double(arma::as_scalar(arma::sum(arma:vectorize(a(span(0, a.n_rows-1)))))) ; return 0 ; }

Corrected C++ code:

int main(int argc, char** argv) { double b; mat a; a = arma::randu(5, 5); b = double(arma::as_scalar(arma::sum(arma::vectorise(a)))); return 0; }

There are 3 issues here: 1) Armadillo uses 'vectorise' instead of 'vectorize' as the function name. I guess it's a typo but ... 2) the generated code missed one column ':' after arma. It should be arma::vectorise(). 3) a(span(0, a.n_rows-1)) is not right since it's a mat. You can simply use a, doesn't need indexing. Like: arma::vectorise(a)

Thanks.

taiwuchiang commented 7 years ago

fixed in /dev2 branch.