Having trouble translating code from Matlab to C++

Everything is solved, thank you
Last edited on
int linspace[4/dx-3/dx+1]; <-- An array size can only be an integer value and this array may not always be an integer. An array can store floating point values, but it's size must be an integer.

double current[i] <-- You cannot declare an array with a variable size. Use something like vectors for this. This array needs to read something like double current[10];

= -0.5+0.5*cos(linspace); <-- linspace is nothing. You must say which element of the array you want to use, linspace[0] or linspace[1], for example.

0;linspace<=2*PI;linspace+dx); <-- linspace is nothing. You must say which element of the array you want to use, linspace[0] or linspace[1], for example.

In fact, that problem occurs a lot.

int linspace <-- is declared again one line later.

current is also declared twice.

I think you should read up on arrays and then correct the above errors and any duplicates of those errors.

need help on translating Matlab code to C++
¿why are you doing that?
A couple of things:
_ Arrays index goes from 0 to n-1 (not 1 to n)
_ Look at std::valarray. There are function overloads that will apply to every element. Also it's got operators. (pretty much like matlab vectors)
_ Variables must be declared (just 1 time). Its type cannot change, so you can't "resize" an array.
___There is however std::valarray or std::vector, their size can change but they will not "expand" automagically.

1
2
for( initialization; condition; increment )
  statement;
i3+dx has no effect. You must assigned to the variable i3 += dx

matlabtocplusplus.cpp:29:21: error: cannot convert 'double' to 'double' in initialization
That error message is incorrect. Issue a bug to your compiler vendor.
It should have been
matlabtocplusplus.cpp:29:21: error: cannot convert 'double*' to 'double' in initialization
double past = current; In this case current is a pointer to the first element of the array. But past is just one value.

Later you try to do past[n]. You can't do that as past is just one value, not an array.

Good luck.
Last edited on
Basically you should know that unlike Matlab you should always declare variables in C/C++ before use.

Also always initialize your variables before use.

You cannot change variable type whenever you like. Try to take this in mind when programming.

For arrays you should pay attention not to exceed boundary limits (no checking here just errors or crash) and to remember as @ne555 that arrays indeces start from 0 n ot 1!
Okay let me ask these questions to clean up my code a little.
How do I say:
set positions (1 through n) = 0 in the array 'current'
current (at position 1) = XXX
current (from X to Y) = XXX

Thank you.
set positions (1 through n) = 0 in the array 'current'

either when you declare the array:

double current[n] = {0};

or after declaring the array:

1
2
double current[n];
memset(current, 0, n*sizeof(double));


current (at position 1) = XXX


You mean at position 1 and not 0 right? If so:

current[1] = XXX;

current (from X to Y) = XXX


1
2
3
4
for (int i = X; i < Y + 1; i++)
{
   current[i] = XXX;
}


Explanation of for loop:
"int i = X": i is the loop counter, and we are starting it at X
"i < Y + 1": continue looping while i < Y +1, we have to say Y + 1 because we are being inclusive of element Y, this is equivalent to i <= Y (for integers at least)
"i++": increment i by one for each iteration of loop, equivalent to i = i +1 and i += 1. If you only wanted to process every other element you would say something like "i += 2"
Last edited on
Thank you Ashishduh, now as for

"i++": increment i by one for each iteration of loop, equivalent to i = i +1 and i += 1. If you only wanted to process every other element you would say something like "i += 2"


can I do something like i+dx to process every .01 element?
Sorry didn't check this over weekend.

can I do something like i+dx to process every .01 element?

Not in this context because there's no such thing as the .01th element of an array, only integers can be array indices (current[1], current[2], etc). But in general, yes you can use floating point numbers in for loop counters:

1
2
3
for (double i = 0.0; i < 10.0; i += 0.1) 
{
}
Last edited on
Topic archived. No new replies allowed.