Convert MATLAB code to R [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a tool for converting MATLAB code to R?
I have a lot of code that needs to be converted from MATLAB to R. It doesn't have to be accurate, but it will be helpful in giving a head start.

Paul Gilbert provides a rough Bash script that could get you started (he claims it will convert about 80% of the way) on the R mailing list:
#!/bin/csh
cp $1 $2
ex -s $2 <<eof
g/%/s//#/g
g/function\(..*\)=\(..*\)(\(..*\)/s//\2 <-function( \3 { \1/
g/end/s// } #/
g/for\(..*\)=\(..*\):\(..*\)/s//for ( \1 in \2 : \3 ) {/
g/_/s//./g
g/;/s///g
g/==/s//##/g
g/=/s//<-/g
g/##/s//==/g
g/zeros(/s//matrix(0,/g
g/ones(/s//matrix(1,/g
g/eye(/s//diag(1,/g
g/\/s//solve(,)/g
g/fsolve('\(..*\)'/s//ms(~\1 /g
g/param(\(..*\))/s//param[ \1 ] /g
g/var(\(..*\))/s//var[ \1 ] /g
g/mod1(\(..*\)/s//mod1[ \1 /g
wq
eof

No there is no easy conversion. Some will translate nearly exactly, some will translate only with great pain and suffering. At least you'll be using R though! Start here to work out analogous functions and syntax:
http://cran.r-project.org/doc/contrib/R-and-octave.txt
http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf
When you get stuck please ask specific questions here. This is really too vague as it stands, though those reference cards will help with getting started.

An alternative to translating the code would be to call MATLAB from within R, using the RMatlab package.
I have not tried RMatlab, but the package description states:
This package provides methods to read
and write MAT files. It also makes it
possible to communicate (evaluate
code, send and retrieve objects etc.)
with Matlab v6 or higher running
locally or on a remote host.

Please see the comments on this issue:
URL:https://mandymejia.wordpress.com/2014/08/18/three-ways-to-use-matlab-from-r/
She mentions several options:
Option 1: Run a single MATLAB command at a time using system()
Option 2: Use R.matlab to send code to the MATLAB server
Option 3: Write an entire MATLAB program using writeLines() and run using system()

Related

Array job based on R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to construct and submit an array job based on R in the HPC of my university.
I'm used to submit array jobs based on Matlab and I have some doubts on how to translate the overall procedure to R. Let me report a very simple Matlab example and then my questions.
The code is based on 3 files:
"main" which does some preliminary operations.
"subf" which should be run by each task and uses some matrices created by "main".
a bash file which I qsub in the terminal.
1. main:
clear
%% Do all the operations that are common across tasks
% Here, as an example, I create
% 1) a matrix A that I will sum to the output of each task
% 2) a matrix grid; each task will use some rows of the matrix grid
m=1000;
A=rand(m,m);
grid=rand(m,m);
%% Tasks
tasks=10; %number of tasks
jobs=round(size(grid,1)/tasks); %I split the number of rows of the matrix grid among the tasks
2. subf:
%% Set task ID
idtemp=str2double(getenv('SGE_TASK_ID'));
%% Select local grid
if idtemp<tasks
grid_local= grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
else
grid_local= grid(jobs*(idtemp-1)+1: end,:); %for the last task, we should take all the rows of grid that have been left
end
sg_local=size(grid_local,1);
%% Do the task
output=zeros(sg_local,1);
for g=1:sg_local
output(g,:)=sum(sum(A+repmat(grid_local(g,:),m,1)));
end
%% Save output by keeping track of task ID
filename = sprintf('output.%d.mat', ID);
save(filename,'output')
3. bash
#$ -S /bin/bash
#$ -l h_vmem=6G
#$ -l tmem=6G
#$ -l h_rt=480:0:0
#$ -cwd
#$ -j y
#Run 10 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 10
#$ -t 1-10
#$ -N Example
date
hostname
#Output the Task ID
echo "Task ID is $SGE_TASK_ID"
export PATH=/xx/xx/matlab/bin:$PATH
matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; subf; exit"
These are my questions:
Suppose I'm able to translate "main" and "subf" into R language. Should I be extra-careful about anything in particular concerning the parallelisation? For example, do I have to declare some parallel environment, such as parLapply or dopar?
In the "main" file I should also install some R packages. Can I do them locally in my folder directly at the beginning of the "main" file, or should I contact the HPC administrator to install them globally?
I could not find any example of bash file for R in the instructions given by my university. Therefore, I have doubts on how to re-adapt the above bash file. I suppose that the only lines to change are:
export PATH=/xx/xx/matlab/bin:$PATH
matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; subf; exit"
Could you give some hints on how I should change them?
The parallelization is handled by the HPC, right? In which case, I think "no", nothing special required.
It depends on how they allow/enable R. In a HPC that I use (not your school), the individual nodes do not have direct internet access, so it would require special care; this might be the exception, I don't know.
Recommendation: if there is a shared filesystem that both you and all of the nodes can access, then create an R "library" there that contains the installed packages you need, then use .libPaths(...) in your R scripts here to add that to the search path for packages. The only gotcha to this might be if there are non-R shared library (e.g., .dll, .so, .a) requirements. For this, either "docker" or "ask admins".
If you don't have a shared filesystem, then you might ask the cluster admins if they use/prefer docker images (you might provide an image or a DOCKERFILE to create one) or if they have preferred mechanisms for enabling various packages.
I do not recommend asking them to install the packages, for two reasons: First, think about them needing to do this with every person who has a job to run, for any number of programming languages, and then realize that they may have no idea how to do it for that language. Second, package versions are very important, and you asking them to install a package may install either a too-new package or overwrite an older version that somebody else is relying on. (See packrat and renv for discussions on reproducible environments.)
Bottom line, the use of a path you control (and using .libPaths) enables you to have complete control over package versions. If you have not been bitten by unintended consequences of newer-versioned packages, just wait ... congratulations, you've been lucky.
I suggest you can add source("main.R") to the beginning of subf.R, which would make your bash file perhaps as simple as
export PATH=/usr/local/R-4.x.x/bin:$PATH
Rscript /path/to/subf.R
(Noting that you'll need to reference Sys.getenv("SGE_TASK_ID") somewhere in subf.R.)

how can i get out of vi editor when esc won't work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 12 months ago.
Improve this question
I am using vi editor for UNIX. Sometimes I am experiencing an issue with getting out of vi editor, where I go to press esc then type ":wq" or "q!" to quit, but it is not escaping. vi editor just enters weird symbols/characters and I can't get out. What do I do to escape and exit when esc won't escape?
There are few more commands for that apart from the one you are using
Shift+zz to save (if modified )and exit
:cq quit without writing
EDIT :
I found some more commands that I just learned .
There are many ways to exit from vi editors, and you can use some of these commands to exit from other editors
Press F2, this will drop you in Insert Mode, now press: q and hit enter
another way is to press Ctrl + c or ctrl + z command to exit the vi editor forcefully.
press ZZ (shift+z+z). it will save and exit.
Ctrl + [ will also work like escape key.
the 3rd , and 6th command may or may not work on every system, as it depends on terminal's setting and system itself.
or you can do map certain keys to behave like escape, please refer this post , that will save you some keystrokes.
For me this worked:
CTL + c (this is equal to ESC in your case)
:wq! (to write save and quit) and then click enter
have you tried ESC then "ZZ"?
seems to work as a last result for me.
This can happen when vi is started with TERM=linux. You can use the set term command to solve this problem.
Set the TERM to vt100 with one of the following commands depending on the shell.
csh or tcsh: setenv TERM vt100
sh: TERM=vt100; export TERM
ksh, bash, or zsh: export TERM=vt100
vi should work without setting the terminal in kx mode(refer to this for a similar issue: https://unix.stackexchange.com/questions/86742/term-linuxxterm-vi-in-an-xterm-or-the-aaabbbbbbccddd-problem).
In order to save this, add the set TERM command to ~/.vimrc file so the option is loaded when starting vi.
Have you tried ^z followed by "kill" the process?
I just had this issue with editor of command line of Git Bash and what worked was press Esc and then :q!. When I typed only q!, it did not work.
press the following shift + esc
look at bottom left corner
write the following colon included :wq

differences between .sage and .spyx in numerical evaluation

the question seems very basic, i'm sorry but i could not find an answer in the documentation.
the content of both files test.sage and test.spyx is identical; it's just
a = 1/sqrt(2)
print a
if i run test.sage with
$ sage test.sage
i get
1/2*sqrt(2)
but the outcome is different from if i run the file test.spyx with
$ sage test.spyx
where i get
Compiling test.spyx...
0.707106781187
how can i prevent sage from numerically evaluating 1/\sqrt(2) in .spyx mode?
(i asked the question on ask.sagemath.org as well but got no answer yet...)

Find the value of a variable and replace it with new value in UNIX [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to access a text file and read some of its contents to change them with a new value.
This Find and Replace is really helpful but my requirement is slightly different.
Say if these are my file contents
image.description=Template Image 08182015
image.version=1.3.111
baseline.name=001_08_18_2015
I want to change them to
image.description=Template Image 08192015 #19 instead of 18
image.version=1.3.112 #112 instead of 111
baseline.name=001_08_19_2015 #19 instead of 18
At any point of time I will not be knowing what is the value of each variable but all I know is the variable name like "Image version" So now I need some script to find what is the value of variable image.version and auto increment the value to the next possible integer.
Any suggestions/ thoughts?
With GNU awk for the 3rd arg to match():
$ awk 'match($0,/(image\.version.*\.)(.*)/,a){$0=a[1] a[2]+1} 1' file
image.description=Template Image 08182015
image.version=1.3.112
baseline.name=001_08_18_2015
For the others since no arithmetic is involved you should be able to just use gensub() similarly.
Or, maybe this is the kind of thing you're looking for:
$ cat tst.awk
BEGIN { FS=OFS="=" }
$1=="image.description" { match($2,/.* ../); $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH,2)+1 substr($2,3+RLENGTH) }
$1=="image.version" { match($2,/.*\./); $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH)+1 }
$1=="baseline.name" { split($2,a,/_/); $2=a[1]"_"a[2]"_"a[3]+1"_"a[4] }
{ print }
$ awk -f tst.awk file
image.description=Template Image 08192015
image.version=1.3.112
baseline.name=001_08_19_2015

Is there a formatter/beautifier for NGINX config files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I know this may sound a little stupid, but one of my NGINX config files is a piece of crap when it comes to formatting. It works and all but that's about it.
I tried to find some kind of beautifier or formatter, like http://jsbeautifier.org/ but then for nginx config files instead of javascript, but no luck so far.
I hope anyone would have a suggestion. There are no requirements, as long as it can format quickly / lazily made NGINX config files!
Thanks!
I found a few projects which might suit your needs:
Nginx Formatter (python) by 1connect
you can get it here
Nginx beautifier (js/nodejs) by vasilevich
nginxbeautifier.com which lets you format configs quickly in a web browser.
you can get a command line tool also on the same site to run it locally.
If your block lines end with {'s and }'s, this simple indenter could help you a bit.
It does not format all your configs, it only fixes indentation.
Original in awk (source):
#!/usr/bin/awk -f
{sub(/^[ \t]+/,"");idx=0}
/\{/{ctx++;idx=1}
/\}/{ctx--}
{id="";for(i=idx;i<ctx;i++)id=sprintf("%s%s", id, "\t");printf "%s%s\n", id, $0}
Or rewritten in python:
INDENT = ' ' * 4
def indent(contents):
lines = map(str.strip, contents.splitlines())
current_indent = 0
for index,line in enumerate(lines):
if (line.endswith('}')):
current_indent -= 1
lines[index] = current_indent * INDENT + line
if (line.endswith('{')):
current_indent += 1
return ('\n').join(lines)
There is a fork of http://jsbeautifier.org/ for nginx here: https://github.com/vasilevich/nginxbeautifier

Resources