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) # submatrices
subm < sapply(subA, mean) # submeans
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
Share :

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 lvalue 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
wish help you to fix your issue Given this code: , You can try 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);
solver.Add(x + dx <= 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());
}
solver.Add(range.ToArray().Sum() <= 10);
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;
}

