Hi, I'm working on a large sparse matrix. I'm wondering if there is a fast way to set elements of a matrix to 0 from one given index (dependent on the row) to the remaining of the row ?
For each row, I have a linear index and I would like to set all the elements from this index to the end of the row to 0. Obviously you can achieve that with a for loop (see the code) but for efficiency reasons, I was wondering if there were a faster solution for that! Any idea?
X % sparse matrix of size m x n
Indexes % Linear Indexes of size m x 1
size_X=size(X); [~,Column_Index]=ind2sub(size_X,Indexes); % get the column number of each Indexes
for i=1:length(Indexes) X(i,Column_Index(i):end)=0;end
For example, if
X =[ 1 3 -1 1 22 2 2 2 3 3 3 3]
and
Indexes =[ 7 2 9]
X should become:
1 3 0 0
0 0 0 0
3 3 0 0
Best Answer