Custom coordinates into an array

5 Ansichten (letzte 30 Tage)
Matthieu
Matthieu am 10 Okt. 2013
Kommentiert: Matthieu am 10 Okt. 2013
Hi,
This problem is getting me really crazy right now.
I'm trying to figure a way to make this work elegantly.
A is a N x N x N x...x N array (N is repeated M times).
N and M are integers greater than 2.
Assume a given i in {1,...,M} and a given j in {1,...N}.
I'm looking for a way to design a function that will receive A,N,M,i,j and will return
A(:,:,...,:,j,:...,:)
where j is at the i-th coordinate.
I.E. all elements who have j as their i-th coordinate, stored into a Nx...xN (M-1 times) array.
I have spent hours trying to find an elegant way to do this.
and still haven't found a lot in matlab's doc (sub2ind might come in handy eventually)
I also tried to design ':,...,:,j,:,...:' as a string and tried to pass it as an argument to A() but it does not seem to work.
I'm a bit stuck in here and I could really use a tip on this !
Thank you for your time !
M

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 10 Okt. 2013
ii = repmat({':'},1,M);
ii(i) = {j};
out = A(ii{:});
  1 Kommentar
Matthieu
Matthieu am 10 Okt. 2013
Good job. Works perfectly fine. First time I hear about this repmat function. Could have saved me from a lot of trouble in previous codes. Thanks !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by