# Extract / Replace Rows / Columns of matrix?

I’m looking for a quick and easy method to do the below four things

Replace P rows of an MN matrix with a PN matrix

Replace P columns of an MN matrix with a MP matrix

Retrieve P rows from an MN matrix into a PN matrix

Retrieve P columns from an MN matrix into a MP matrix

All the above apply to dense column major matrices - and assume that I have an idx vector to extract/replace the rows/columns like below

idx=[0,3,5,7,8,9,10,16,16,17]

So to rephrase, I’m looking for an easy to code and quick to run method to
M.getcols(idx) creates a matrix M10 containing the 0th,3rd,5th…17th columns
M.setrows(idx) Puts the rows of the 10
N matrix into the 0th,3rd,5th,…17th rows of M

Thanks!

I’m an idiot. It’s actually quite trivial. kernel on the way…

``````template<bool reverse>
__global__ void idx_op(float *out, const float *A, int m, const int *idx0, const int *idx1, int p0, int p1)
{
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int skip = blockDim.x * gridDim.x;
for (int i = tid; i < p0*p1; i += skip)
{
int read1 = i/p0;
int read0 = i - p0 * read1;
int down = idx0[read0];
int across = idx1[read1];
if (reverse) out[down+m*across] = A[i];
else out[i]=A[down+m*across];
}
}
``````

out = output matrix
A = read matrix
m = size of leading dimension of A
idx0 = vertical index
idx1 = horizontal index
p0 = size of idx0
p1 = size of idx1