Raza Javed
Raza Javed

Reputation: 65

How to read some fixed number of elements from an array in octave?

I have an array of 102300000 elements. and I want to make two arrays from the elements of this array like this:

  1. One array should have the elements from the following indices of the original array: index = 1-20,41-60, 81-100, 121-140 and so on
  2. The other should have the elements from the following indices of the original array: index = 21-40, 61-80, 101-120, 141-160 and so on

Does anyone have an idea how to implement this?

Upvotes: 2

Views: 203

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

A quick vectorised way to do this is to realise that the desired zero-indices for the top array are the ones where the modulus by 20 is the same as the modulus by 40, and that the other array is one where the two moduluses differ. Therefore, you can make use of this fact to perform 'logical indexing'.

Because octave is not zero-based indexing, you have to subtract one from the one-based index.

E.g.

Indices = 1:200;
M = Indices * 10;   % our example matrix; multiplied by 10 here
                    % simply to differentiate it visually from the Indices
M1 = M( mod( Indices - 1, 20 ) == mod( Indices - 1, 40 ) );
M2 = M( mod( Indices - 1, 20 ) != mod( Indices - 1, 40 ) );

Upvotes: 3

Related Questions