YAS
YAS

Reputation: 303

Subtracting rows and columns from a vector at non-zero indices MATLAB

Suppose I have the following matrix in MATLAB:

A =[0    0    4    0; 
    0    5    0    3; 
    1    2    0    0];

given the following vectors:

b1 = [1 2 3];
b2 = [2 3 4 5];

the output should look like this:

C1 =[0    0    3    0; 
     0    3    0    1; 
    -2   -1    0    0];

C2 =[0    0    0    0; 
     0    2    0   -2; 
    -2   -1    0    0];

C1 and C2 are column-wise and row-wise subtraction of the original matrix A from vectors happening at non-zero elements. Note A in reality is sparse matrix. Obviously answers without using loop is appreciated! Thank you

Upvotes: 0

Views: 229

Answers (3)

beaker
beaker

Reputation: 16791

This one might be a little more memory efficient:

A =[0    0    4    0; 
    0    5    0    3; 
    1    2    0    0];

b1 = [1 2 3].';   % transpose so it's a column vector
b2 = [2 3 4 5].';

[Arows Acols Avals] = find(A);
C1 = sparse([Arows;Arows], [Acols;Acols], [Avals;-b1(Arows)]);
C2 = sparse([Arows;Arows], [Acols;Acols], [Avals;-b2(Acols)]);

Results:

>> full(C1)
ans =

   0   0   3   0
   0   3   0   1
  -2  -1   0   0

>> full(C2)
ans =

   0   0   0   0
   0   2   0  -2
  -1  -1   0   0

This takes advantage of the fact that sparse adds the values given for duplicate subscripts. A can be sparse or full.

Upvotes: 2

NKN
NKN

Reputation: 6424

No need to use a loop. First perform the subtractions and then replace the elements that should remain 0.

C1 = A - repmat(b1.',1,size(A,2));
C2 = A - repmat(b2,size(A,1),1);
C1(A==0)=0;
C2(A==0)=0;

C1 =

     0     0     3     0
     0     3     0     1
    -2    -1     0     0

C2 =

     0     0     0     0
     0     2     0    -2
    -1    -1     0     0

Test on Sparse Matrix

You can also confirm that this will work on Sparse Matirces

A = sparse(10,10);
A(5:6,5:6)=rand(2);
b1 = rand(10,1);
b2 = rand(1,10);

B1 = A - repmat(b1,1,size(A,2));
B2 = A - repmat(b2,size(A,1),1);

B1(A==0)=0;
B2(A==0)=0;

Upvotes: 1

Tony Tannous
Tony Tannous

Reputation: 14866

C1 = A ~= 0; // save none zero elements of A
b1 = b1.';   // transpose b1
b1 = [b1, b1, b1, b1];  // create matrix of same size as A
C1 = C1.*b1;            
C1 = A-C1;

C1:

 0     0     3     0
 0     3     0     1
-2    -1     0     0

Next is C2

 C2 = A ~= 0;
 k = [b2; b2; b2];
 C2 = C2.*k;
 C2 = A-C2;

C2:

 0     0     0     0
 0     2     0    -2
-1    -1     0     0

Upvotes: 0

Related Questions