Extracting columns from a matrix with a matrix

10 views (last 30 days)
I have a matrix like that A=[0 3 0; 0 2 1]. I want to extract the information from a B matrice which has many columns. I want matrixes (in this case it is 2 matrixes) that will sum up the columns in the B matrix with respect to A matrix. For example, 1st matrix will be the summation of 0th 3rd and 0th column (of B matrix), 2nd matrix will be the summation of 0th 2rd 1st column (of B matrix). And i want to name these matrixec as X1 , X2 etc.. A matrix is just an example here, the matrix can differ in row and column size. How can i do this? Thank you.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 5 Jun 2017
B = randi(10,6,8);
A=[0 3 0; 0 2 1];
X = num2cell(sum(reshape(B(:,A+1),size(B,1),size(A,1),[]),3),1);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!