Though this doesn't answer your question about for
loops, I would avoid using loops all together and create column-major linear indices to access into your matrix. Use sub2ind
to help facilitate that. sub2ind
takes in the size of the matrix in question, the row locations and column locations. The output will be an array of values that specify the column-major locations to access in your matrix.
Therefore:
A = ones(3); i = [1 2 3]; j = [3 2 1]; %// Your code
%// New code
ind = sub2ind(size(A), i, j);
A(ind) = 0;
Given that you have a table, you can perhaps convert the table into an array, apply sub2ind
on this array then convert the result back to a table when you're done. table2array
and array2table
are useful tools here. Given that your table is stored in A
, you can try:
Atemp = table2array(A);
ind = sub2ind(size(Atemp), i, j);
Atemp(ind) = 0;
A = array2table(Atemp);