Multidimensional Indices of Multiple pages
Show older comments
Hi, I have a matrix of dimensions 5 by 7 by 50 e.g. g1 = rand(5,7,50) I have another matrix of indices for the row and column e.g. [1 3; 4 5; etc] Now I would like to index all the pages at those indices:
My attempt albeit slow:
for page = 2:50
lg = sub2ind(size(g1), ind(:,1), ind(:,2), page.*ones(length(ind),1),1))
g1(lg)
end
Could someone explain to me how to do the above without for loop. I think I can follow one of the link below to remove the sub2ind. Thx!
In addition to the following I did look at the doc
1 Comment
Sean de Wolski
on 30 Apr 2012
+1 well written, well researched question. Welcome to MATLAB Answers!
Accepted Answer
More Answers (2)
Geoff
on 30 Apr 2012
You could use linear indexing if you convert each row/column into a page start index, then take the page range....
g1 = rand(5,7,50);
xy = [1 3; 4 5; 2 1];
% Get the first page index for each xy pair.
start = xy(:,1) + (xy(:,2)-1) * size(g1,1);
% Generate linear index across all pages.
makerange = @(x) x:size(g1,1)*size(g1,2):numel(g1);
indices = cell2mat( arrayfun( @makerange, start, 'UniformOutput', false ) );
% Pull out the pages. One page per row, corresponding to the rows in xy.
pages = g1(indices);
I'm a MatLab newbie, so there might be clever functions I don't know about that do some of this work for you. That effectively does what you are asking though. =)
3 Comments
Wing
on 30 Apr 2012
Geoff
on 1 May 2012
Bummer. I didn't think that _anything_ was slower than MatLab loops! =)
Sean de Wolski
on 1 May 2012
@Geoff, common misperception. Since the advent of the JIT compiler for-loops are often faster than vectorized methods.
Sean de Wolski
on 30 Apr 2012
perhaps I'm not clear but if:
A = bsxfun(@times,ones(5,7),reshape(1:50,1,1,50)) %each slice is its index
and you want to extract all slices at rows 3,4 and cols, 4,5, just use the colon operator on the 3rd dimension
A([3 4],[4 5],:)
And you are correct, sub2ind will likely be slower than a well written for-loop.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!