Borrar filtros
Borrar filtros

Assign multidimensional array 'slice' or 'chunk'

2 visualizaciones (últimos 30 días)
David Russell
David Russell el 4 de Sept. de 2017
Respondida: Guillaume el 4 de Sept. de 2017
What’s the best way to assign
A(:, :, x1, x2, , xn)
to a given array, where
x = [x1, x2, , xn]
is a vector whose length is arbitrary (but matches the relevant dimensions of A)?

Respuestas (1)

Guillaume
Guillaume el 4 de Sept. de 2017
Use sub2ind. However, your vector will have to be repmat'ed for each colon in your indexing. e.g.:
x = [4, 7, 8]
A = reshape(1:5*6*7*8*9, [5, 6, 7, 8, 9]);
%indexing wanted: A(:, :, x1, x2, x3)
[idx1, idx2] = ndgrid(1:size(A, 1), 1:size(A, 2)); %generate all valid indices for the 1st two colons, put all colons into the ndgrid call
idxx = arrayfun(@(v) repmat(v, size(idx1), 1), x, 'UniformOutput', false); %repmat each x value into shape of colon index matrices and transform into cell array
result = A(sub2ind(size(A), idx1, idx2, idxx{:}))
isequal(result, A(:, :, 4, 7, 8))

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by