  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 can i build a for function for matrix?  » r » How can i build a for function for matrix?

By : Camila Santos
Date : November 19 2020, 12:41 AM
wish of those help Now i have a list like below: , You don't need a for loop for that. You can use lapply: code :
``````## First, make up some sample data
set.seed(1)
mylist <- replicate(2, sample(0:1, 9, TRUE), FALSE)

## Let's work on a copy in case you need the original
m <- mylist
m
# []
#  0 0 1 1 0 1 1 1 1
#
# []
#  0 0 0 1 0 1 0 1 1

## Here's the actual transformation
m[] <- lapply(m, matrix, nrow = 3, byrow = TRUE)
m
# []
#      [,1] [,2] [,3]
# [1,]    0    0    1
# [2,]    1    0    1
# [3,]    1    1    1
#
# []
#      [,1] [,2] [,3]
# [1,]    0    0    0
# [2,]    1    0    1
# [3,]    0    1    1
`````` ## Is there a build-in function to create a matrix m such that m(r,c) = fun(r,c)

By : mihe
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Just to make it clear, there is no generic "vectorized" solution if fun does not accept vectors (or matrices) for input.
code :
``````[Y, X] = meshgrid(1:R, 1:C);
m = arrayfun(@(r, c)fun(r, c), X, Y)
`````` ## Vectorize numpy indexing and apply a function to build a matrix

By : Cane Corso Puppy
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a matrix X of size (d,N). In other words, there are N vectors with d dimensions each. For example, , Here's an approach -
code :
``````# Get a flattened version of indices
idx0 = np.concatenate(I)

# Get indices at which we need to do "intervaled-summation" along axis=1
cut_idx = np.append(0,map(len,I))[:-1].cumsum()

# Finally index into cols of array with flattend indices & perform summation
``````
``````In : X
Out:
array([[ 1,  2,  3,  4],
[15,  6, 17,  8]])

In : I
Out: array([[0, 2, 3, 1], [2, 3, 1], [2, 3]], dtype=object)

In : idx0 = np.concatenate(I)

In : idx0 # Flattened indices
Out: array([0, 2, 3, 1, 2, 3, 1, 2, 3])

In : cut_idx = np.append(0,map(len,I))[:-1].cumsum()

In : cut_idx # We need to do addition in intervals limited by these indices
Out: array([0, 4, 7])

In : X[:,idx0]  # Select all of the indexed columns
Out:
array([[ 1,  3,  4,  2,  3,  4,  2,  3,  4],
[15, 17,  8,  6, 17,  8,  6, 17,  8]])

Out:
array([[10,  9,  7],
[46, 31, 25]])
`````` ## How to build & store this large lower triangular matrix for matrix-vector multiplication?

By : wodes
Date : March 29 2020, 07:55 AM
wish helps you You don't always need to explicitly form a matrix to do a matrix-vector or matrix-matrix multiplication. For example, no one really forms a diagonal matrix and use it for such computations.
There is no substantial difference between your matrix and a diagonal matrix.
code :
``````MatVecMul <- function (y, k) {
n <- length(y)
z <- numeric(n)
for (i in 1:n) z[i:n] <- z[i:n] + k ^ (i - 1) * y[1:(n - i + 1)]
z
}
``````
``````d <- structure(c(1, 0.9, 0.81, 0.729, 0.6561, 0, 1, 0.9, 0.81, 0.729,
0, 0, 1, 0.9, 0.81, 0, 0, 0, 1, 0.9, 0, 0, 0, 0, 1), .Dim = c(5L, 5L))
set.seed(0); y <- runif(5)
c(d %*% y)
# 0.8966972 1.0725361 1.3374064 1.7765191 2.5070750

MatVecMul(y, 0.9)
# 0.8966972 1.0725361 1.3374064 1.7765191 2.5070750
``````
``````library(Rcpp)
cppFunction("NumericVector MatVecMul_cpp (NumericVector y, double k) {
int n = y.size();
NumericVector z(n);
int i; double *p1, *p2, *end = &z[n];
double tmp = 1.0;
for (i = 0; i < n; i++) {
for (p1 = &z[i], p2 = &y; p1 < end; p1++, p2++) *p1 += tmp * (*p2);
tmp *= k;
}
return z;
}")

MatVecMul_cpp(y, 0.9)
# 0.8966972 1.0725361 1.3374064 1.7765191 2.5070750
``````
``````v <- runif(1e4)
system.time(MatVecMul(y, 0.9))
#   user  system elapsed
#  3.196   0.000   3.198
system.time(MatVecMul_cpp(y, 0.9))
#   user  system elapsed
#  0.840   0.000   0.841
`````` ## Java function to build the following square matrix

By : nati55575
Date : March 29 2020, 07:55 AM
Hope that helps I need a function which builds the following matrix, taking (int n) as function argument:
code :
``````1

1 3
4 2

1 4 8
6 2 5
9 7 3

1  5 11 15
8  2  6 12
13  9  3  7
16 14 10  4

1  6 14 20 24
10  2  7 15 21
17 11  3  8 16
22 18 12  4  9
25 23 19 13  5

1  7 17 25 31 35
12  2  8 18 26 32
21 13  3  9 19 27
28 22 14  4 10 20
33 29 23 15  5 11
36 34 30 24 16  6

1  11  29  45  59  71  81  89  95  99
20   2  12  30  46  60  72  82  90  96
37  21   3  13  31  47  61  73  83  91
52  38  22   4  14  32  48  62  74  84
65  53  39  23   5  15  33  49  63  75
76  66  54  40  24   6  16  34  50  64
85  77  67  55  41  25   7  17  35  51
92  86  78  68  56  42  26   8  18  36
97  93  87  79  69  57  43  27   9  19
100  98  94  88  80  70  58  44  28  10
`````` ## Build a new matrix made of some source matrix rows given a row index vector

By : user2991736
Date : March 29 2020, 07:55 AM
Hope that helps With the development branch and (at least) C++11 enabled, you can write:
code :
``````Eigen::MatrixXi N = m(r1,Eigen::all);
``````
``````N = m(r1, :);
``````
``````std::vector<int> c2{{3,0}};
std::cout << "m({2,1},c2) = \n" << m({2,1}, c2) << '\n';
``````
``````m({2,1}, c2) = Eigen::Matrix2i{{1,2},{3,4}};
std::cout << m << '\n';
`````` 