Accessing the nth dimension in a variable sized multidimensional array

10 vues (au cours des 30 derniers jours)
Jan Bode
Jan Bode le 13 Juil 2020
Commenté : Walter Roberson le 15 Juil 2020
I have a large array of values, and before the execution I don't know how many dimension I need.
How can I extract a single column from this? Or even a specific value?
I tried things like writing a vector of the indices, but that didn't return a single value.
Example: I have a matrix A, how do I get A(1,1,...,1,:) when I don't know how many dimensions A has?
And what if I want to set a single dimension to a specific value like A(1,:,:,...,:)
Alternatively: is there another good way of storing high-dimensional scalar fields? I tried a list of index vectors like in a sparse matrix, but finding a specific value becomes very inefficient that way right?

Réponse acceptée

Stephen23
Stephen23 le 13 Juil 2020
Modifié(e) : Stephen23 le 13 Juil 2020
"...how do I get A(1,1,...,1,:) when I don't know how many dimensions A has?"
You use a comma-separated list, which you can easily generate from a cell array:
C = {1,1,1,...1,,':'};
A(C{:})
You can create the cell array using repmat or num2cell or similar. The function ndims will also be useful here.
"...what if I want to set a single dimension to a specific value like A(1,:,:,...,:)"
Use a comma-separated list. Lets try a more practical example, where we define the size of C automatically:
C = repmat({':'},1,ndims(A));
C{1} = 1; % C{dim} = index
val = A(C{:})
  4 commentaires
Walter Roberson
Walter Roberson le 14 Juil 2020
subsref twice? That does not sound right. end calls the end method; https://www.mathworks.com/help/matlab/matlab_oop/object-end-indexing.html
Jan Bode
Jan Bode le 15 Juil 2020
Well okay, guess I will do it manually then

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 13 Juil 2020
nd = ndims(YourArray);
dim_wanted = randi(nd);
proto = repmat({1}, 1, nd);
proto{dim_wanted} = ':';
just_that_dim = YourArray(proto{:});
dim_one = randi(nd);
proto = repmat({':'}, 1, nd);
proto{dim_one} = 1;
all_except_that_dim = YourArray(proto{:});
proto = arrayfun(@randi, size(YourArray), 'uniform', 0);
dim_wanted = randi(nd);
proto{dim_wanted} = ':';
random_vector_in_N_space = YourArray(proto{:});
  1 commentaire
Walter Roberson
Walter Roberson le 15 Juil 2020
My Answer showed how to index by any arbitrary dimension: repmat() the most common index, and replace the specific choices, then use cell expansion as the index expression. You could make a helper function, such as
IndexDim = @(A,n,index) A(struct('t',[repmat({':'},1,n-1),index,repmat({':'},1,ndims(A)-2+(n==1))]).t)
Note: doing this on one line requires a fairly new version of MATLAB; I do not recall at the moment if it needs R2020a or R2019b, but R2019a would be expected to fail for this.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by