Basic matrix math in R [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I want to perform basic math using two matrices.
As my input I have two 3x3 matrices. I would like to divide every integer in matrixA by every integer in matrixB and the output to be one 3x3 matrix of their products.
What is the R function(s) that can do that for me.
Thanks in advance!

It sounds like you want to divide element by element. In this case, you can simply use the / operator.
### Create two matrices
matA <- matrix(1:9, nrow = 3)
matB <- matA
### Divide element by element
matB / matA
### As Frank pointed out, division by 0 goes to Inf
matA[1, 1] <- 0
matB / matA

Related

Number of cells of raster under a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a binary raster file and I want to count the number of pixels with value one. How do I do that with R?
Ben Bolker's solution, but then with terra (the replacement for raster)
set.seed(101)
library(terra)
r <- rast(matrix(sample(0:1,size=10000,replace=TRUE),100))
global(r, sum)
# sum
#lyr.1 4984
To count the number of 1s in non-binary rasters, and considering NAs, I would prefer
global(r==1, sum, na.rm=TRUE)
over
global(r, function(x) sum(x==1, na.rm=TRUE))
Because the former also works on very large rasters, as "sum" is a known function that can be computed in chunks.
There may well be a more efficient way to do this, but:
set.seed(101)
library(raster)
r <- raster(matrix(sample(0:1,size=10000,replace=TRUE),100))
cellStats(r,function(x,...) sum(x==1)) ## 4984
Actually, if this is a binary raster, cellStats(r, sum) works just as well (and probably quicker?)

How to do elements wise addition or multiplication R with 4 X 3 and 3 X 5 vector in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to do element wise multiplication of two matrix a and b I'm getting the error saying
Error in a + b : non-conformable arrays
data <- matrix(151:162, nrow=4)
data2 <- matrix(221:235, nrow=3)
Error in a * b : non-conformable arrays
However when I'm doing actual matrix multiplication I'm getting the desired output. Can anyone suggest me how to fix that.
150:162 are 13 elements and 220:235 are 16. length(150:162) will show. These aren't 4x3 or 3x5 matrices.
Update: For elementwise operation the dimensions of the matrices need to be identical. See size(data). So these operations are not possible with 3x4 and 4x3.

Function for matrix in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm new (very new) in R. I'm struggling with making a function that's supposed to take a matrix (old_matrix) and return a new matrix (new_matrix), but in new_matrix all values in old_matrix that is a prime should be multiplied by 2 when it appears in new_matrix. So the new matrix should look the same as the old matrix, but where a prime occurs in old, this element should be multiplied by 2.
I'm thinking that I should start out with a for loop, but I'm already struggling with how to make the loop go through all elements of the matrix. I appreciate all the help I can get to get closer to making this function!
The isPrime function in the numbers package could be a big help
# Start by creating an example to work with
old_matrix <- matrix(sample.int(100, 25), 5, 5)
# Create your new matrix and determine which numbers are prime
new_matrix <- old_matrix
primeVals <- numbers::isPrime(old_matrix)
# Index into the matrix using the prime value indicator and multiply by 2
new_matrix[primeVals] <- new_matrix[primeVals]*2

Colwise sum of matrix in a sequential order [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 6 years ago.
Improve this question
I would like to do colwise sum of matrix that follow a particular sequence. For example, if I have a matrix of 50 rows, the first four rows will be added in a colwise manner, then 2 to 5 rows, 3 to 6, ... etc. following that pattern. How can I do this in R?
set.seed(123)
mat <- matrix(sample(100,50*10,replace=TRUE),nrow=50)
n <- nrow(mat)
sapply(1:(n-3), function(i) colSums(mat[i:(i+3),]))
#Update
oddInd <- sapply(1:(n-3), function(i) {ind <-i:(i+3); ind[!!ind%%2] })
evenInd <- sapply(1:(n-3), function(i) {ind <-i:(i+3); ind[!ind%%2] })

Sliding window using R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a data frame with daily data in R (148 columns by 6230 rows). I want to find the correlations coefficients using sliding windows with length of 600 (days) with windows displacement of 5 (days) and trying to generate 1220 correlation matrices (approx.). All the examples that I saw used only one information vector. There exist an easy way to find those correlation matrices using sliding window? I'll appreciate any suggestion.
If M is the input matrix then each row of out is one correlation matrix strung out column by column:
library(zoo)
out <- rollapply(M, 600, by = 5, function(x) c(cor(x)), by.column = FALSE)
They could be reshaped into a list of correlation matrices, if need be:
L <- lapply(1:nrow(out), function(i) matrix(out[i, ], ncol(M)))
or as an array:
simplify2array(L)

Resources