From: Joshua Arnott on
"james bejon" <jamesbejon(a)yahoo.co.uk> wrote in message <hoiotq$ifq$1(a)fred.mathworks.com>...
> It does help. Thanks very much. But how, then, can I use a vector to reference elements of a matrix the way I'm trying to do? That is, suppose I have
>
> v = [2, 1, 1, 1];
>
> and I want to use v to reference
>
> data(:, :, 2, 1, 1, 1)
>
> Can this be done (generically)? I'm currently experimenting with doing it by reshaping data as a 2d matrix (with the pages tiled, if you know what I mean) and then selecting the appropriate one. But it feels like there should be an easier way.

James,

You can use:

data(:,:,v(1),v(2),v(3),v(4))

Alternatively, use a single variable making use expanded indexing as demonstrated by Bruno.

Josh.
From: us on
"james bejon" <jamesbejon(a)yahoo.co.uk> wrote in message <hoiotq$ifq$1(a)fred.mathworks.com>...
> It does help. Thanks very much. But how, then, can I use a vector to reference elements of a matrix the way I'm trying to do? That is, suppose I have
>
> v = [2, 1, 1, 1];
>
> and I want to use v to reference
>
> data(:, :, 2, 1, 1, 1)
>
> Can this be done (generically)? I'm currently experimenting with doing it by reshaping data as a 2d matrix (with the pages tiled, if you know what I mean) and then selecting the appropriate one. But it feels like there should be an easier way.

one of the solutions

% the data
m=rand([2,3,4,2]);
m(:,:,3,2)=-1;
v=[3,2];
% the engine
v=num2cell(v);
r=m(:,:,v{:});
% the result
disp(r);
%{
-1 -1 -1
-1 -1 -1
%}

us
From: james bejon on
Thanks US. That's a nice solution.