Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Filling up a matrix with computed values of another matrix

Assuming that the block of columns are based on the first two characters, i.e. A1, A2, we can split this into different blocks by using substr to extract the first two characters from the column names and use this as index to split. Then, we can either use apply with range and diff to get the result or use pmax and pmin.

  indx <- substr(colnames(df), 1,2)

If the grouping is not based on the column names but on the position, this should also work

  indx <- (1:ncol(df)-1)%/%4 +1

  res1 <- sapply(split(seq_len(ncol(df)),
df[,i, drop=FALSE]))


 res2 <-
sapply(split(seq_len(ncol(df)), indx),
            function(i) apply(df[,i, drop=FALSE],
diff(range(x))) )

 identical(res1, res2)
 #[1] TRUE
 #        A1       A2
 #[1,] 1.680105 2.443593
 #[2,] 7.701992 2.447799
 #[3,] 7.740707 1.367992

Or using your code

 newmatrix <- matrix(0, nrow(df), 2)
#here the example dataset is only 7 columns
 for(i in (1:nrow(df))) newmatrix[i,1] <- 
 for(i in (1:nrow(df))) newmatrix[i,2] <- 
 #    [,1]     [,2]
 #[1,] 1.680105 2.443593
 #[2,] 7.701992 2.447799
 #[3,] 7.740707 1.367992

If you have many blocks of columns, you can try a double for loop

 lst <- split(seq_len(ncol(df)),
indx) #keep the columns to group in a `list`
 newmatrix <- matrix(0, nrow(df), 2) #he
 for(i in 1:nrow(df)){
 for(j in seq_along(lst)){
   newmatrix[i,j] <- diff(range(df[i,

#      [,1]     [,2]
#[1,] 1.680105 2.443593
#[2,] 7.701992 2.447799
#[3,] 7.740707 1.367992


df <- structure(list(A1B1 =
c(-0.37014356, 3.03017573, 3.50183121), 
A1B2 = c(1.08841141, 1.39812421, 1.51249433), A1B3
= c(-0.126574243, 
0.243516558, -0.775449944), A1B4 = c(-0.5916936,
-4.2388756), A2B1 = c(1.682673457, -0.378640756,
), A2B2 = c(-0.427706432, 2.039940436,
0.431838943), A2B3 = c(-0.76091938, 
-0.40785893, 0.91108052)), .Names = c("A1B1",
"A1B2", "A1B3", 
"A1B4", "A2B1", "A2B2", "A2B3"), class =
"data.frame", row.names = c(NA, 

Categories : R

Related to : Filling up a matrix with computed values of another matrix
python - how to compute correlation-matrix with nans in data-matrix
You can convert all nan values to zeros using np.nan_to_num() and then proceed further. Demo: >>> data array([[ 5.1, 3.5, 1.4, 0.2], [ 4.9, 3. , nan, 0.2], [ 4.7, 3.2, 1.3, nan], [ nan, 3.1, 1.5, 0.2]]) >>> np.cov(data.T) array([[ nan, nan, nan, nan], [ nan, 0.04666667, nan, nan],

Categories : Python
Eigen sparse matrix multiplications seem to compute full matrix
Basically, the document is sightly confusing for me at least. the way to do it is simply: SpMat mat_3 = mat_1 * mat_2 No dense matrix is created along the way. Eigen rocks!

Categories : C++
matrix inside matrix when multiplying symbolic matrices
The problem seems to be the K = C2^(16/57). Taking the root of a matrix is not trivial and solutions don't always exist. That's why MATLAB cannot resolve this expression. Try for example: A = syms('A',[3,3]); % create 3x3 symbolic matrix B = A^(1/3); % calculate the 3rd root of A The result will be ans = matrix([[a1_1, a1_2, a1_3], [a2_1, a2_2, a2_3], [a3_1, a3_2, a3_3]])^(1/3)

Categories : Matlab
Select sets of values from R matrix column
Try H[cbind(seq_len(nrow(H)), P)] ## [1] 0.6733731 0.7396847 0.5953580 Here we are indexing by consecutive rows and columns indicated in P Regarding your question, so the reason H[, P] returns a matrix is because you are telling R: select all rows in columns: 2, 1, 2 from matrix "H" thus the result that you are getting is a matrix with identical first and third columns.

Categories : R
R dealing with NULL values while converting list to matrix
You could try the new stri_list2matrix function in the stringi package. library(stringi) stri_list2matrix(lapply(data, unlist), byrow=TRUE, fill="") # [,1] [,2] [,3] # [1,] "A, B" "F" "" # [2,] "C" "M" "RIP" Or for NA instead of "", leave out the fill argument stri_list2matrix(lapply(data, unlist), byrow=TRUE) # [,1] [,2] [,3] # [1,] "A, B" "F" NA # [2,] "C" "M"

Categories : R
Recently Add
How to handle blank values while reading into r
Is it possible to have sortable(Interactive) table in rMarkdown?
Remove border when ggsave svg is embedded in html file
using paste with a list
Transforming data frame into a range label in r
Loop through clip tiff files and export to new folder
Plotting shinyTable data
Casting multiple columns from one factor variable
code issue with developing a sentiment analysis scoring model
Scale relative to a value in each group (via dplyr)
Making a new variable column using if/else statements
How do I find last date in which a value increased in another column?
R - week function returns unexpected values
Why does rownames(installed.packages()) have a names attribute?
Shiny for R - conditional comparing text input
Labels for Bars in R Lattice Bar Chart
plot each column of a dataframe on the same line graph in r
R create adjacency matrix according to columns from data.frame
dplyr + adding replicated vector via 'mutate'
R: data frame - How to add up values and consolidate duplicate values?
Covariance Parameters for Krig in geoR ksline
Retrieve/access dynamic variable from R function
Create Simulated Data Multiple Regression in R
Write results from stepAIC to a table
labeling row and col names when using dist() and as.matrix()
R: Issues with line graph of germination trough time
what is the "unscaled variance" in r's linear model summary?
Working with dates and abline(v) in R
knitr and rgl: inconsistent size?
Binary variable from multiple character string variable
© Copyright 2017 Publishing Limited. All rights reserved.