-3

I'm manually converting a code in C to MATLAB.

The code contains assignments like the following,

y[10] = p[32]+(p[31]-p[32])*pow(p_c[0],p[34])/(pow(p_c[0],p[34])+pow(p[33],p[34]));

To use it in MATLAB, I am manually increasing the value of all indexes by 1, since the index in MATLAB starts from 1.

y[10+1] = p[32+1]+(p[31+1]-p[32+1])*pow(p_c[0+1],p[34+1])/(pow(p_c[0+1],p[34+1])+pow(p[33+1],p[34+1]));

Is there an easy way of doing this task? There are around 30 assignments like the above example and I am trying to avoid doing this manually.

Edit 1: Would it be possible to use regular expression? I can copy all the lines of code that contain assignment in a text file. Using regular expression, if I can locate [35](any number) replace with [35+1]. I'm not sure how to implement this in a code.

Edit 2:A sample of other assignments in the code.

  y[0] = ct[0]-x[12];
    y[1] = ct[1]-1*x[10]-x[23]; 
    y[3] = p_c[6]+p_c[5];   
    y[4] = p_c[2];  
    y[5] = x_c[23]+x_c[10]+y_c[1];  
    y[6] = y_c[0]+x_c[12];  
    p[0] = 30;  
    p[1] = 12;  
    p[2] = 2;
    p[3] = 0;
    p[4] = 90;  
    p[5] = 45
    dx[0] = FunctionForD(p[67], p[64], p[66], p[65], p[23], x_c[0], x_c[3], p[49])*p[23]-FunctionForA(y[28], y[29], p[23], y[16])*p[23]+FunctionForD(y[30], y[31], p[23], y[16])*p[23]-FunctionForA(p[134], p[133], p[132], p[130], p[131], p_c[2], p[23], x_c[21], x_c[0], p[49])*p[23];   // 
    dx[1] = FunctionFor2(p[169], p[167], p[168], p[166], p[23], x_c[1], x_c[17], p[49])
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Natasha
  • 937
  • 2
  • 13
  • 30
  • 2
    For all those who is wondering why she has to increase index by 1 is in Matlab array indexing starts from 1(Crazy matlab). – kiran Biradar Sep 18 '18 at 06:37
  • Would it be possible to use regular expression? I can copy all the lines of code that contain assignment in a text file. Using regular expression, if I can locate `[35]`(any number) replace with `[35+1]`. I'm not sure how to do this. – Natasha Sep 18 '18 at 06:44
  • 1
    If it just 30 assignments I would manually do it. – kiran Biradar Sep 18 '18 at 06:46
  • Please clarify: do you want to replace `[0]` with `[0+1]` or with `[1]` (evaluated sum)? What tool/programming language are you using? – Wiktor Stribiżew Sep 18 '18 at 07:33
  • 1
    A simple `sed` command would do the job: `sed -e s/\[\([0-9]*\)\]/[1+\1]/g` – Mathieu Sep 18 '18 at 07:35
  • 1
    `sed -e 's/\[\([0-9]*\)\]/[\1+1]/g' temp.txt > output.txt` Corrected the syntax from @purplepsycho – kiran Biradar Sep 18 '18 at 07:54
  • @WiktorStribiżew I want to replace with the evaluated sum. I use python and I'm a beginner in MATLAB. – Natasha Sep 18 '18 at 08:32
  • Then the answer below does not work for you, why accept? Notepad++ does not allow evaluating replacements. Or are you doing that all manually? – Wiktor Stribiżew Sep 18 '18 at 08:33
  • @WiktorStribiżew The regex option in Notepad++ worked for me. It doesn't give the evaluated sum though. – Natasha Sep 18 '18 at 08:35
  • @WiktorStribiżew Since I mentioned replacing [0] with [0+1] in the original post.I accepted the answer below. – Natasha Sep 18 '18 at 08:37

1 Answers1

1

If you were dealing with just one array, you could come up a way to do this. But here you are dealing with 8 different arrays (y, ct, x, p_c, x_c, y_c, p, dx). And the assignments too are in no particular order. They involve various combinations.

If you are using Linux/Unix, you can use the stream editor (sed) tool to accomplish this. For windows, Notepad++ (which is free) supports regex search and replace. Have a look at this link.

If it is just 30 assignments better do it manually. You can ensure that each index is correctly incremented by 1 in MATLAB by doing something like this:

#define BMI 1 /* BMI is BASE_MATLAB_INDEX */

y[0 + BMI] = ct[0 + BMI] - x[12 + BMI];

and so on...

P.W
  • 24,743
  • 6
  • 32
  • 69