Skip to content

Instantly share code, notes, and snippets.

View nilforooshan's full-sized avatar

Mohammad Ali Nilforooshan nilforooshan

View GitHub Profile
@nilforooshan
nilforooshan / mrotate.md
Last active March 30, 2019 09:55
R: Rotate matrix

Rotate a matrix -90/90 degrees

Amat is the input matrix.

Rotate 90 degrees

Bmat = t(Amat)
for(i in 1:nrow(t(Amat))) Bmat[i,] = t(Amat)[nrow(t(Amat))+1-i,]
@nilforooshan
nilforooshan / mflip.md
Last active March 30, 2019 09:55
R: Flip a matrix

Flip a matrix

Amat is the input matrix.

Flip horizontally

Bmat = Amat
for(i in 1:nrow(Amat)) Bmat[i,] = Amat[nrow(Amat)+1-i,]
@nilforooshan
nilforooshan / deducttimes.md
Last active September 2, 2019 21:21
R: Deduct times

R function to deduct times

The function:

DeductTimes = function(timeA, timeB) # "HH:MM:SS", timeA - timeB
{
   Atime = unlist(strsplit(timeA, "[:]"))
   Btime = unlist(strsplit(timeB, "[:]"))
 HHded = as.integer(Atime[1]) - as.integer(Btime[1])
@nilforooshan
nilforooshan / sumtimes.md
Last active March 30, 2019 09:58
R: Sum times

R function to sum times

The function:

SumTimes = function(timeA, timeB) # "HH:MM:SS"
{
   Atime = unlist(strsplit(timeA, "[:]"))
   Btime = unlist(strsplit(timeB, "[:]"))
 HHsum = as.integer(Atime[1]) + as.integer(Btime[1])
@nilforooshan
nilforooshan / print_lower_diag.md
Last active March 30, 2019 10:00
R: Print lower.tri-diag (with/without diags) of a matrix

R function for printing lower.tri-diag (with/without diags) of a matrix

The function:

lpLdiag <- function(x,diags=TRUE)
{
    x[upper.tri(x, diag=!diags)] <- NA
    print(x)
    for(i in 1:nrow(x)) print(c(na.omit(x[i,])))
@nilforooshan
nilforooshan / primes.md
Last active March 30, 2019 10:03
R: Find prime numbers in an array

R function to find prime numbers in an array

The functions:

check.integer <- function(x) { x == round(x) }
 
is.prime <- function(y) { !check.integer(y/2) &
                          !check.integer(y/3) &
 !check.integer(y/5) &amp;
@nilforooshan
nilforooshan / SQmatrix_reorder.md
Last active March 30, 2019 10:03
R: Re-order a square matrix

R function for re-ordering a square matrix

The function:

SQmat_reorder <- function(inpmat, oldorder, neworder) # Input matrix (inpmat), oldorder & neworder inputs
{
   colnames(inpmat) = row.names(inpmat) = oldorder
   outmat <- inpmat[match(neworder, oldorder), match(neworder, oldorder)]
 return(outmat)
@nilforooshan
nilforooshan / ageindays.exe
Last active March 30, 2019 10:05
f90: Worried about your age? Calculate it in days! Calculations according to: http://mathforum.org/library/drmath/view/59234.html *.exe and *.out are the Windows and Linux executables.
@nilforooshan
nilforooshan / chisq_test.exe
Last active March 30, 2019 10:07
f90: Pearson's Chi-square test. Test the frequency distributions of categorical variables in a table of frequencies. *.exe and *.out are the Windows and Linux executables.
@nilforooshan
nilforooshan / corr_cov.exe
Last active March 30, 2019 10:07
f90: Convert correlation and covariance matrices to each other. *.exe and *.out are the Windows and Linux executables.