Filter löschen
Filter löschen

Linearly increasing groups of indices

1 Ansicht (letzte 30 Tage)
Nils
Nils am 11 Mär. 2013
Hello, I think I can explain my question most easily with an example. If I only want to access every 10th element in an array a, then I can easily do this by a(1:10:end). But let's say that I am instead looking for groups of entries whose indices all are increasing with 10 all the way to the final index. For instance, access the entries with indices 1,3,4,11,13,14,21,23,24... Naturally a([1 3 4]:10:end) does not accomplish this. Is there a way that I through indexing could do this without a loop?
Thanks in advance!
  1 Kommentar
Nils
Nils am 12 Mär. 2013
Actually, this is only to be done offline with a few matrices of moderate dimensions (part of setting up an MPC problem) so I was looking for something that was syntactically convenient and not necessarily the fastest. Thanks all for good suggestions. I appreciate your time and creativity!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 11 Mär. 2013
Bearbeitet: Jan am 12 Mär. 2013
A = 1:100;
B = reshape(A, 10, 10);
C = reshape(B([1,3,4], :), 1, []);
[EDITED] Perhaps Bruno's tool can help: FEX: mcolon.
[EDITED 2] Another approach, which does not limit the size of the input to be a multiple of the step width:
A = 1:100;
index = false(1, numel(A));
index(1:10:end) = true;
index(3:10:end) = true;
index(4:10:end) = true;
C = A(index);
Note: This is equivalent to Sean's approach, but avoids the sorting by using logical indexing.
If you want to write this more compact, use a function to conceal the details:
function C = (A, start, step)
len = numel(A);
index = false(1, len);
for k = 1:length(start)
index(start(k):step:len) = true;
end
C = A(index);
  4 Kommentare
Jan
Jan am 12 Mär. 2013
Bearbeitet: Jan am 12 Mär. 2013
@Nils: Correct, if numel(A) is not a multiple of 10, the reshaping fails.
I do not think that this is a natural problem. But this is caused by my personal experiences only, because I haven't had such a demand in the last 35 years, and haven't read an equivalent question in the forums in the last 10 years.
But see [EDITED].
@Sean: Of course RESHAPE is such fast, because it performs almost nothing. Only the tiny vector of dimensions is changed after chekcing, that the product of the new dimensions equals the number of elements. Therefore in my opinion STRUCT2CELL is faster, because it does tocuh the data - at least I have though that. But surprisingly the internal representation of structs and cells differs in one detail only: The struct has an additional #fields*64 char array for the field names. So even here the speed is a result of a smart representation of the data.
Finally I think, a*ones(n,m) is the fastest operation: My measurements seem, like this avoids even the multiplication, such that it is faster than repmat(a, n, m).
Sorry for comparing apples with beers.
Sean de Wolski
Sean de Wolski am 12 Mär. 2013
@Nils, you can always just use a for-loop if numel(A) is not a multiple of 10.
I would be very surprised if this is the bottleneck in your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sean de Wolski
Sean de Wolski am 11 Mär. 2013
A = 1:100;
A(sort([1:10:end, 3:10:end,4:10:end])) = pi
Of course a for-loop might be faster.
  2 Kommentare
Nils
Nils am 11 Mär. 2013
Of course that's not a bad answer at all! I was hoping for some built-in indexing syntax though, that supported this operation.
Jan
Jan am 11 Mär. 2013
Sorting is expensive. This works only, if all subvectors have the same length:
n = length(A);
index = [1:10:n; 3:10:n; 4:10:n];
A(index(:))

Melden Sie sich an, um zu kommentieren.


Andrei Bobrov
Andrei Bobrov am 12 Mär. 2013
out = bsxfun(@plus,[1;3;4],0:10:10*(floor(x(end)/10)-1));

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by