function-style array indexing
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ben Mitch
am 13 Mär. 2013
Bearbeitet: per isakson
am 5 Aug. 2016
Hi
This problem keeps coming up for me over the years, and there must be a standard solution, so perhaps someone knows it, I can't ever google it up.
Say I have an array A of any number of dimensions, and I want to apply an indexing operation to the last dimension. Now, if it's 2D, I just use:
B = A(:, ii);
If it's 3D, I use:
B = A(:, :, ii);
but what if it's n-D? Afaik there is no syntax in the language for this, so I'd have to use some function. A function "index_into()", for instance, to give:
B = index_into(A, n, ii);
At the moment, I always end up doing some horrible mash like:
X = permute(A, [ndims(A) ...]);
X = X(ii, :);
X = reshape(X, ...);
B = permute(X, ...);
Surely there must be a better way...? Any suggestions?
Cheers
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 13 Mär. 2013
idxcell = repmat({':'}, 1, ndims(A));
idxcell{n} = ii;
A(idxcell{:})
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!