Translation of matlab vectors code into r

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Translation of matlab vectors code into r

Haio
This post was updated on .
Hi there
I am new user of r, i would need some help to translate som code for vectors in matlab to r. I have managed to translate the first 7 rows, but not the rest. Could anyone give me any suggestions for this problem??


Matlab code:

tempo=[];
temps=[];
tempn=[];
tempao=[];
tempas=[];
tempan=[];
for k=1:5
    tempo = [tempo n_o(k,:)];
    temps = [temps n_s(k,:)];
    tempn = [tempn n_n(k,:)];
   
    tempao = [tempao nanst_o(k,:)];
    tempas = [tempas nanst_s(k,:)];
    tempan = [tempan nanst_n(k,:)];
end.
This is the code that i´m trying to translate into r, so far i have managed to translate the first 7 rows into r, but not last 6 rows.

R-Code:
 tempo= vector()
 temps= vector()
 tempn= vector()
 
 tempao= vector()
 tempas= vector()
 tempan= vector()
             for (k in 5:1)
?
?
?
?
?
?



Reply | Threaded
Open this post in threaded view
|

Re: Translation of matlab vectors code into r

Michael Weylandt
Perhaps read An Intro to R (shipped with every packaging of R -- type
help.start() to get it) and then look at

http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf

n_o, nan_s, etc. are not functions I have in my Matlab distribution so
I can't give more advice without knowing what they actually do.

Your loop is wrong though: 1:5 in Matlab translates to 1:5 in R, not 5:1

Michael

On Sun, May 6, 2012 at 7:07 AM, Haio <[hidden email]> wrote:

> Hi there
> I am new user of r, i would need some help to translate som code for vectors
> in matlab to r. I have managed to translate the first 7 rows, but not the
> rest. Could anyone give me any suggestions for this problem??
>
>
> Matlab code:
>
> tempo=[];
> temps=[];
> tempn=[];
> tempao=[];
> tempas=[];
> tempan=[];
> for k=1:5
>    tempo = [tempo n_o(k,:)];
>    temps = [temps n_s(k,:)];P
>    tempn = [tempn n_n(k,:)];
>
>    tempao = [tempao nanst_o(k,:)];
>    tempas = [tempas nanst_s(k,:)];
>    tempan = [tempan nanst_n(k,:)];
> end.
> This is the code that i´m trying to translate into r, so far i have managed
> to translate the first 7 rows into r, but not last 6 rows.
>
> R-Code:
>  tempao= vector()
>  tempas= vector()
>  tempan= vector()
>
>  tempao= vector()
>  tempas= vector()
>  tempan= vector()
>             for (k in 5:1)
> ?
> ?
> ?
> ?
> ?
> ?
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Translation-of-matlab-vectors-code-into-r-tp4612656.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Translation of matlab vectors code into r

Hans W Borchers
In reply to this post by Haio
Haio <joakim.aback <at> gmail.com> writes:

>
> Hi there
> I am new user of r, i would need some help to translate som code for vectors
> in matlab to r. I have managed to translate the first 7 rows, but not the
> rest. Could anyone give me any suggestions for this problem??
>
> Matlab code:
>
> tempo=[];
> temps=[];
> tempn=[];
> tempao=[];
> tempas=[];
> tempan=[];
> for k=1:5
>     tempo = [tempo n_o(k,:)];
>     temps = [temps n_s(k,:)];
>     tempn = [tempn n_n(k,:)];
>
>     tempao = [tempao nanst_o(k,:)];
>     tempas = [tempas nanst_s(k,:)];
>     tempan = [tempan nanst_n(k,:)];
> end.
> This is the code that i´m trying to translate into r, so far i have managed
> to translate the first 7 rows into r, but not last 6 rows.
>

This is extremely bad Matlab code, I would discourage you to translate it
line by line. What the code does:

It takes each of the matrices n_o, n_s, n_n, nanst_o, nanst_s, nanst_n
and combines the first five lines into vectors each. A way to do this in R
could be as follows:

    tempo <- c(t(n_o[1:5, ]))
    # etc.

where n_o, etc. will be the same matrices as in your Matlab example.
A similar solution, of course, would be possible in Matlab.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.