Using the function sub2ind
to create a linear index is the typical solution to this problem, as shown in this closely-related question. You could also compute a linear index yourself instead of calling sub2ind
.
However, your case may be simpler than those in the other questions I linked to. If you’re only ever indexing a single point with your current_point
vector (i.e. it’s just an n-element vector of subscripts into your n-dimensional matrix), then you can use a simple solution where you convert current_point
to a cell array of subscripts using the function num2cell
and use it to create a comma-separated list of indices. For example:
current_point = [1 2 3 ...]; % A 1-by-n array of subscripts
subCell = num2cell(current_point); % A 1-by-n cell array of subscripts
output_matrix(subCell{:}) = val; % Update the matrix point
The operation subCell{:}
creates the equivalent of typing subCell{1}, subCell{2}, ...
, which is the equivalent of typing current_point(1), current_point(2), ...
.