I would like to index an N-dimensional array with a vector of length N. In particular, for the 2-dimensional case I am currently doing the following.
A = rand(10);
v = randi(10,1,2);
v = num2cell(v);
A(v{:})
However, this approach seems horribly inefficient. Is there not a smarter way to convert the vector v to a proper index (comma-separated) for the array A?
Edit Let's say v = randi(10,1,2) = [ 3 5 ]. In that case, I want to obtain A(3,5).

4 Kommentare

Stephen23
Stephen23 am 7 Aug. 2015
Bearbeitet: Stephen23 am 7 Aug. 2015
This could be the best solution already, for an array of arbitrary number of dimensions. Why do you think that this method "seems horribly inefficient"? Did you run the profiler on your code and find that this operation took the most time?
Jori
Jori am 7 Aug. 2015
Bearbeitet: Jori am 7 Aug. 2015
I did indeed run the profiler on the code and I found that the operation took quite some time. If I compare it against Walter Roberson his method, I obtain a nice improvement, see the figure. (p1 and p2 are identical)
You can improve performance slightly by assigning the transpose to p1_off ahead of time, equivalent to if I had written
Aoff = cumprod([1 As(1:end-1)]) .';
and then just use * Aoff rather than * Aoff.'
Also the "-1" was important to add to the indices. It probably can be rolled into the calculation of the offset at the expense of clarity.
Jori
Jori am 7 Aug. 2015
Thanks, that is a nice remark.
In these computations, data(n,2:end) has values starting at 0. I need to add 1 to adjust for the minimal index 1 in Matlab. This is followed by the "-1" that you had and so the "-1" disappears in line 25 of the profiler figure.
I verified the results, and this is indeed the correct indexing.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Aug. 2015

2 Stimmen

%setup
As = size(A);
Aoff = cumprod([1 As(1:end-1)]);
%calculation at run-time
vidx = (v-1) * Aoff.' + 1;
A(vidx)
This will work when v is an M x N array of indices, producing a column array of M values. If your array has trailing singular dimensions being indexed then the code will not work as-is (the last entry of Aoff needs to be duplicated for all trailing singular dimensions.)
The calculation here converts the index components into linear indices using some trivial matrix multiplication.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 7 Aug. 2015
Bearbeitet: Azzi Abdelmalek am 7 Aug. 2015

0 Stimmen

A = rand(10);
v = randi(10,1,2)
Why converting to cell array? just type
A(v)
If v is a cell array, convert it to a double array
A = rand(10);
v = randi(10,1,2);
v = num2cell(v);
A([v{:}])

4 Kommentare

Jori
Jori am 7 Aug. 2015
Because that is not what I want. Let's say v = [ 3 5 ]. A(v) gives me [ A(3) A(5) ]. However, I want A(3,5). Hope that clears it up. I'll edit my post.
Azzi Abdelmalek
Azzi Abdelmalek am 7 Aug. 2015
You can use sub2ind function
Azzi Abdelmalek
Azzi Abdelmalek am 7 Aug. 2015
Bearbeitet: Azzi Abdelmalek am 7 Aug. 2015
A = rand(10)
v = randi(10,1,2)
idx=sub2ind(size(A),v(1),v(2))
A(idx)
If v is nx2 array
idx=sub2ind(size(A),v(:,1),v(:,2))
A(idx)
Jori
Jori am 7 Aug. 2015
Bearbeitet: Jori am 7 Aug. 2015
The size of v is 1 x N. I do not know N beforehand.
Using idx = sub2ind(size(A),v) does not work. So I would need to use idx = sub2ind(size(A),v(1),v(2),...,v(N)), but I do not know the value of N beforehand. Thus, I would need to use
v = num2cell(v);
idx = sub2ind(size(A),v{:});
A(idx)
Which brings me back to the original problem.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Gefragt:

am 7 Aug. 2015

Kommentiert:

am 7 Aug. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by