C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# How to perform row mean in matrix by 3 number of columns in sequence using R

By : We Got Served
Date : November 19 2020, 03:01 PM
Hope that helps I have a matrix with dimension 100x300 where I want to reduce number of columns, 300, into 100 columns by averaging per 3 columns, i.e. columns 1-3, become the new column 1, columns 4-6 becomes the new column 2, and so forth. So that my 100x300 matrix becomes a 100x100 matrix. , Here's an example of averaging 9 columns into 3 columns
code :
``````#DATA
set.seed(42)
m = matrix(rnorm(90), ncol = 9)

sapply(split(sequence(NCOL(m)), ceiling(sequence(NCOL(m))/3)), function(i) rowMeans(m[,i]))
#                1          2           3
# [1,]  0.78972984  0.3277913  0.03411781
# [2,] -0.01978707 -0.1466863  0.11765521
# [3,] -0.39921655  1.1229981  0.43126071
# [4,]  0.52291618 -0.2309106  0.10843898
# [5,]  0.72204682 -0.2578551 -0.82148326
# [6,]  0.03311892 -0.3358800  0.83184534
# [7,]  0.32333323 -0.3055211  0.29562900
# [8,] -1.50475918  0.2276755  0.43983899
# [9,]  0.01268471 -1.9462480  0.32276620
#[10,]  0.20580146  0.3255511  0.14762346
``````

Share :

## How to extract two columns from a matrix in a sequence of 6 columns in matlab

By : Frank Air
Date : March 29 2020, 07:55 AM
Hope that helps Basically I have a <96x659 double> matrix and I want to extract 1st and 2nd column , then 8th and 9th, then 15th and 16th column and so on.. So I want each 2 columns in a step of 6 . I hope I was clear enough. I'm newbie in matlab . in advance! , All you really need to do is construct the list of columns you want:
code :
``````columns = [1:7:size(matrix,2)+1,  2:7:size(matrix,2)+1];
submat = matrix(:, columns);
``````
``````submat = matrix(:, sort(columns));
``````

## Change matrix columns and get the number of lines of a sub-matrix with all elements equals

By : Christoph Gummersbac
Date : March 29 2020, 07:55 AM
To fix this issue The problem you mention can be known as the Largest Square Block Problem
You can find several topics related to this with solutions :
code :
``````typedef struct s_max
{
t_uint16 val_max_;
t_uint16 idx_max_;
} t_max;

t_max largest_square_block(t_uint8 *grid, const t_uint16 h, const t_uint16 w)
{
t_uint16 up, upleft, left;
t_uint16 val_max = 0;
t_uint16 idx_max = 0;

t_uint16 *counter = (t_uint16 *) malloc(w * h * sizeof(t_uint16));
counter[0] = grid[0];

for (t_uint i = 1; i < w * h; i ++)
{
up = upleft = left = 0;
if (grid[i] != YOURMAGICNUMBER)
{
if (i >= w)
up = counter[i - w];
if (i % w > 0)
{
left = counter[i - 1];
if (i >= w)
upleft = counter[i - 1 - w];
}
counter[i] = 1 + min3_uint16(up, upleft, left);
if (counter[i] > val_max)
{
val_max = counter[i];
idx_max = i;
}

}
}

free(counter);

t_max data_max;
data_max.val_max_ = val_max;
data_max.idx_max_ = idx_max;

return data_max;
}
``````

## Perform operation on grouped columns of a matrix

By : Malcolm C
Date : March 29 2020, 07:55 AM
I hope this helps you . Suppose I have a matrix M and vector G of integers, such that G has as many entries as there are columns in M. For example (schematically): , Here is one possibility:
code :
``````## I know your matrix has 4 rows, that is what the `4` in the following means
lst <- split(M, rep(G, each = 4L))
sapply(lst, function (x) sapply(split(x, 1:4), max) )

#   1  2  3
#1 20 71 64
#2 88 80 86
#3 31 84 46
#4 46 76 67
``````

## perform calculation on all combinations of columns of a matrix

By : WP II
Date : October 13 2020, 06:00 AM
I think the issue was by ths following , One option is to define a function to apply for two columns and then use outer to apply to all combination of columns.
code :
``````fun <- function(x, y) {
cosine(mat[, x], mat[, y])
}

outer(seq_len(ncol(mat)), seq_len(ncol(mat)), Vectorize(fun))

#       [,1]   [,2]   [,3]   [,4]   [,5]  .....
#[1,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#[2,] 0.7824 1.0000 0.7824 1.0000 0.7824 .....
#[3,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#[4,] 0.7824 1.0000 0.7824 1.0000 0.7824 .....
#[5,] 1.0000 0.7824 1.0000 0.7824 1.0000 .....
#....
``````

## How to perform a calculation for all columns of a matrix?

By : Mark Taylor
Date : October 03 2020, 11:00 PM
I hope this helps you . I have a matrix with tree ring width for 66 trees. Rows represent the years of measurement and columns represent the different trees. , Using base R, we can use lapply
code :
``````treegrowth[paste0(names(df[-1]), "_growth")] <- lapply(treegrowth[-1],
function(x) c(NA, x[-1]/x[-length(x)]))

#  year tree1 tree2 tree3 tree1_growth tree2_growth tree3_growth
#1 1900  0.72  0.34  1.34           NA           NA           NA
#2 1901  0.56  0.88  0.98    0.7777778    2.5882353    0.7313433
#3 1902  1.23  0.56  1.67    2.1964286    0.6363636    1.7040816
``````
``````library(dplyr)
treegrowth %>% mutate_at(-1, list(growth = ~./lag(.)))
``````
``````treegrowth <- structure(list(year = 1900:1902, tree1 = c(0.72, 0.56, 1.23),
tree2 = c(0.34, 0.88, 0.56), tree3 = c(1.34, 0.98, 1.67)),
class = "data.frame", row.names = c(NA, -3L))
``````