Use sub2ind , it converts your subindexes to linear indexes, which is a number pointing to one exact place in ( more ).
Z = [ 1 2 3 ; 4 5 6 ; 7 8 9]; rowNos = [2, 3]; colNos = [2, 1]; lin_idcs = sub2ind(size(Z), rowNos, colNos)
If you want to work with all the elements in a specific row and column (elements in higher dimensions), you can also access them using linear indexing. It gets a little harder to figure them out:
Z = reshape(1:4*4*3,[4 4 3]); rowNos = [2, 3]; colNos = [2, 1]; siz = size(Z); lin_idcs = sub2ind(siz, rowNos, colNos,ones(size(rowNos))); % just the first element of the remaining dimensions lin_idcs_all = bsxfun(@plus,lin_idcs',prod(siz(1:2))*(0:prod(siz(3:end))-1)); % all of them lin_idcs_all = lin_idcs_all(:); Z(lin_idcs_all) = 0;
experiment a bit with sub2ind and go through my code to understand it.
It would be simpler if this were the first dimension that you wanted to take from all elements, then you could use the colon operator :
Z = reshape(1:3*4*4,[3 4 4]); rowNos = [2, 3]; colNos = [2, 1]; siz = size(Z); lin_idcs = sub2ind(siz(2:end),rowNos,colNos); Z(:,lin_idcs) = 0;
source share