  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 # Apply function to each submatrix  » r » Apply function to each submatrix

By : Ermir Bajraktari
Date : November 21 2020, 01:01 AM
will help you Here is a way. It assumes the function being applied returns a scalar, like it is the case with mean: code :
``````A <- matrix(1:16, 4, 4)
k <- 2L

i <- (row(A) + 1L) %/% k        # row grouping
j <- (col(A) + 1L) %/% k        # col grouping
b <- i + (j - 1L) * max(i)      # block grouping
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    3    3
# [2,]    1    1    3    3
# [3,]    2    2    4    4
# [4,]    2    2    4    4

subA <- split(A, b)             # sub-matrices
subm <- sapply(subA, mean)      # sub-means
out  <- array(subm[b], dim(A))  # final output
#      [,1] [,2] [,3] [,4]
# [1,]  3.5  3.5 11.5 11.5
# [2,]  3.5  3.5 11.5 11.5
# [3,]  5.5  5.5 13.5 13.5
# [4,]  5.5  5.5 13.5 13.5
`````` ## return reference to a submatrix from function in numpy

By : Ia Pereira
Date : March 29 2020, 07:55 AM
To fix the issue you can do how I can ... pass reference to the submatrix out of function?
Simply put, you cannot return an l-value from a function call in Python like you can in C++. In your case, Python assumes that you are assigning a value directly to the sub(a), which is a function call, and not to the object returned by it.
code :
``````def sub(a):
a[:2, :2] = np.arange(4).reshape((2,2))
return a[:2, :2]
``````
``````>>> x = sub(a)
>>> x[:] = np.ones((2,2))
>>> x
array([[1, 1],
[1, 1]])
>>> a
array([[1, 1, 2],
[1, 1, 5],
[6, 7, 8]])
``````
``````>>> sub(a)[:] = np.ones((2,2))
>>> a
array([[1, 1, 2],
[1, 1, 5],
[6, 7, 8]])
``````
``````>>> x = 2         # this won't change a because x is a reference passed by value
>>> a
array([[1, 1, 2],
[1, 1, 5],
[6, 7, 8]])
`````` ## Armadillo C++: Passing a submatrix into a function

By : user2977895
Date : March 29 2020, 07:55 AM
This might help you Change the input type to a const reference (const arma::mat&), which allows the compiler to automatically generate a temporary matrix in such cases.
Example:
code :
``````using namespace arma;

mat foo(const mat& my_matrix)
{
mat y = my_matrix * 2.0;

return y;
}

void bar()
{
mat x(10,10, fill::randu);

mat y = foo( x.col(0) );

y.print("y:");
}
`````` ## R apply Return submatrix

By : user3079203
Date : March 29 2020, 07:55 AM
code :
``````do.call(`c`,apply(splitData, 1, function(x) list(test[x,])))
``````
``````lapply(seq_len(nrow(splitData)), function(i)  test[unlist(splitData[i,]),])
`````` ## Get dynamic submatrix and apply constraints

By : LiwaeL
Date : March 29 2020, 07:55 AM
I wish this help you The way to get a value from an array based on an IntVar, inside the solver, is by using the MakeElement() function and in this case the 2d version of it.
That way you can get a specific value from the matrix but not a range based on two IntVars (for example x - dx of rectangles). To accomplish the range part you can use a loop and a ConditionalExpression() to figure out if the specified value is in range.
code :
``````int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
IntVar x = solver.MakeIntVar(0, data.Length - 1);
IntVar dx = solver.MakeIntVar(1, data.Length);

IntVarVector range = new IntVarVector();
for (int i = 0; i < dx.Max(); i++)
{
range.Add(solver.MakeConditionalExpression((x + i < x + dx).Var() , solver.MakeElement(data, (x + i).Var()), 0).Var());
}

``````
``````class DataValues: LongLongToLong
{
private int[,] _data;
private int _rows;
private int _cols;

public DataValues(int[,] data, int rows, int cols)
{
_rows = rows;
_cols = cols;
_data = data;
}

public override long Run(long arg0, long arg1)
{
if (arg0 >= _rows || arg1 >= _cols)
return 0;

return _data[arg0, arg1];
}
}
`````` ## How to pass submatrix to another function as a pointer

By : swoo0day
Date : March 29 2020, 07:55 AM
will be helpful for those in need You cannot do this using only pointer arithmetic. What you have is an array of arrays. Assuming outer array holds rows and inner arrays hold the elements (columns) of each row, you can pass a pointer to the middle of the outer array to skip rows but you cannot skip columns. If you need to skip columns as well you have two options:
code :
``````elem **get_submatrix(elem **mat, int n, int x, int y, int m)
{
elem **submat = (elem **)calloc(m, sizeof(elem*));
int i;
for(i = 0; i < m; i++)
{
submat[i] = mat[y++] + x;
}
return submat;
}
`````` 