Access matrix value using a vector of coordinates?

做~自己de王妃 提交于 2019-12-01 20:56:58
wakjah

You can do this by converting your vector b into a cell array:

B = num2cell(b);
A(B{:}) = 5;

The second line will expand B into a comma-separated list, passing each element of B as a separate array index.

Generalization

If b contains coordinates for more than one point (each row represents one point), you could generalize the solution as follows:

B = mat2cell(b, size(b, 1), ones(1, size(b, 2)));
A(sub2ind(size(a), B{:}))

Here b is converted into cell array, each cell containing all the coordinates for the same dimension. Note that A(B{:}) won't produce the result we want (instead, this will select all elements between the top left and bottom right coordinates), so we'll have to do an intermediate step of converting the coordinates to linear indices with sub2ind.

The straightforward way to do it would be:

A(b(1), b(2), b(3)) = 5;

Another way would be to convert the coordinates to a linear index, similar to function sub2ind:

idx = [1, cumprod(size(A))] * [b(:) - 1; 0] + 1;
A(idx) = 5;

This solution can be further extended for multiple points, the coordinates of which are stored in the rows of b, and the assigned values in vector vals, that equals in length to the number of rows of b:

idx = [1, cumprod(siz(2:end))] * (reshape(b, [], ndims(A)) - 1)' + 1;
A(idx) = vals;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!