Anna
Anna

Reputation: 2845

Get the indices along each dimension of a 3D matrix

Is there a neater way to get the indices along each dimension of a 3D matrix? This is my solution, but I don't like its repeating and taking up three lines.

rows   = 1:size(vol,1);
cols   = 1:size(vol,2);
slices = 1:size(vol,3);

Upvotes: 2

Views: 54

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

you have various options, but it's not really simpler than what you have.

% example volumen
vol = flow(10);

% Option 1
[rows cols slices] = deal( 1:size(vol,1), 1:size(vol,2), 1:size(vol,2) )

% Option 2
indexvectors = cellfun( @(x) 1:size(vol,x), num2cell(1:3), 'uni',0 )

% Option 3
indexvectors = arrayfun( @(x) {1:size(vol,x)}, 1:3)
indexvectors = arrayfun( @(x) {1:x}, size(vol) )

The first returns three single vectors and the latter two options return a cell array with a vector for each dimension in each cell.

Upvotes: 4

Related Questions