Reputation: 724
Let's say I have a 3x3x3 Matlab array with members 1 to 27
a=reshape(1:27, [3 3 3])
I would like to create a subset of this with a syntax like
b=a(range1,range2,range3)
where for range1=range2=range3=1:2 I would get the members b(1,1,1)
and b(2,2,2)
. i.e
b= [1 14]
Is it possible to do this just with indexing and without any functions (e.g. diag)? Thanks...
Upvotes: 0
Views: 1370
Reputation: 13610
The indexing can be done using sub2ind
,
a(sub2ind(size(a),[1:2],[1:2],[1:2]))
if you want to avoid all functions, you could calculated the linear indices yourself...
Upvotes: 1
Reputation: 9075
It can be done with sub2ind
function as follows:
b=a(sub2ind(size(a),range1,range2,range3))
ans: b=[1 14]
Upvotes: 1