Use a vector as an index to a matrix
Solution 1
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), ...
.
Solution 2
I know it is too late but for anybody who will find this topic. the easiest way that work for me is to use: diag(A (x(:),y(:)) )
;
unfortunately this works only if you need to get values from the matrix, not for changing values
Solution 3
You can use the sub2ind
function to get the linear index from the subscript.
Example:
A=magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
selectElement={2,3}; %# get the element at position 2,3 in A.
indx=sub2ind(size(A),selectElement{:});
A(indx)
ans =
10
In the above example, I've stored the subscripts (can be any number of dimensions) as a cell
. If you have it stored as a vector, simply use num2cell()
to convert it to a cell.
You can now easily assign a value to this as A(indx)=value;
. I've used different variables than yours to keep the answer general, but the idea is the same and you just need to replace the variable names.
You also mentioned in your post that you're looping from (1,1,1)
till some value, (1000,15,3)
and assigning a value to each of these points. If you're looping along the columns, you can replace this entire operation with a vectorized solution.
Let finalElement={1000,15,3}
be the final step of the loop. As before, find the linear index as
index=sub2ind(size(A),finalElement{:});
Now if you have the values you assign in the loop stored as a single vector, values
, you can simply assign it in a single step as
A(1:index)=values;
Paul
Background is in physical chemistry and NMR instrumentation, now working as a software developer.
Updated on December 13, 2020Comments
-
Paul over 3 years
I'm writing a MATLAB function to read out data into an n-dimensional array (variable dimension size). I need to be able to access a specific point in the Matrix (to write to it or read it, for example), but I don't know ahead of time how many indexes to specify.
Currently I have a
current_point
vector which I iterate through to specify each index, and amax_points
vector which specifies the size of the array. So, if for example I wanted a 3-dimensional array of size 1000-by-15-by-3,max_points = [1000 15 3]
, andcurrent_point
iterates from[1, 1, 1]
to[1000, 15, 3]
([1, 1, 1]
->[1000, 1, 1]
->[1, 2, 1]
->[1000, 2, 1]
->...). What I'd like to be able to do is feedcurrent_point
as an index to the matrix like so:output_matrix(current_point) = val
But apparently something like
output_matrix([1 2 3]) = val
will just setoutputmatrix(1:3) = 30
. I can't just use dummy variables because sometimes the matrix will need 3 indexes, other times 4, other times 2, etc, so a vector of variable length is really what I need here. Is there a simple way to use a vector as the points in an index? -
Leo about 10 yearsCan you explain this further, I dont understand it but it looks like it could be a nice and simple solution