Note that there are some explanatory texts on larger screens.

plurals
  1. POUse a vector to index a matrix without linear index
    primarykey
    data
    text
    <p>G'day, I'm trying to find a way to use a vector of [x,y] points to index from a large matrix in MATLAB. Usually, I would convert the subscript points to the linear index of the matrix.(for eg. <a href="https://stackoverflow.com/questions/5904488/use-a-vector-as-an-index-to-a-matrix-in-matlab">Use a vector as an index to a matrix</a>) However, the matrix is 4-dimensional, and I want to take all of the elements of the 3rd and 4th dimensions that have the same 1st and 2nd dimension. Let me hopefully demonstrate with an example:</p> <pre><code>Matrix = nan(4,4,2,2); % where the dimensions are (x,y,depth,time) Matrix(1,2,:,:) = 999; % note that this value could change in depth (3rd dim) and time (4th time) Matrix(3,4,:,:) = 888; % note that this value could change in depth (3rd dim) and time (4th time) Matrix(4,4,:,:) = 124; </code></pre> <p>Now, I want to be able to index with the subscripts (1,2) and (3,4), etc and return not only the 999 and 888 which exist in <code>Matrix(:,:,1,1)</code> but the contents which exist at <code>Matrix(:,:,1,2)</code>,<code>Matrix(:,:,2,1)</code> and <code>Matrix(:,:,2,2)</code>, and so on (IRL, the dimensions of <code>Matrix</code> might be more like <code>size(Matrix) = (300 250 30 200)</code></p> <p>I don't want to use linear indices because I would like the results to be in a similar vector fashion. For example, I would like a result which is something like:</p> <pre><code>ans(time=1) 999 888 124 999 888 124 ans(time=2) etc etc etc etc etc etc </code></pre> <p>I'd also like to add that due to the size of the matrix I'm dealing with, speed is an issue here - thus why I'd like to use subscript indices to index to the data.</p> <p>I should also mention that (unlike this question: <a href="https://stackoverflow.com/questions/1146719/accessing-values-using-subscripts-without-using-sub2ind?rq=1">Accessing values using subscripts without using sub2ind</a>) since I want all the information stored in the extra dimensions, 3 and 4, of the i and jth indices, I don't think that a slightly faster version of <code>sub2ind</code> still would not cut it..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload