matlab - Access matrix value using a vector of coordinates? -
let's have vector:
b = [3, 2, 1]; let's have matrix this:
a = ones([10 10 10]); i want use vector b source of coordinates assign values matrix a. in example equivalent to:
a(3, 2, 1) = 5; is there easy way in maltab use vector source of coordinates indexing matrix?
you can converting vector b cell array:
b = num2cell(b); a(b{:}) = 5; the second line expand b comma-separated list, passing each element of b separate array index.
generalization
if b contains coordinates more 1 point (each row represents 1 point), generalize solution follows:
b = mat2cell(b, size(b, 1), ones(1, size(b, 2))); a(sub2ind(size(a), b{:})) here b converted cell array, each cell containing coordinates same dimension. note a(b{:}) won't produce result want (instead, select elements between top left , bottom right coordinates), we'll have intermediate step of converting coordinates linear indices sub2ind.
Comments
Post a Comment