PutsandCalls
PutsandCalls

Reputation: 1011

Not sure why my loop is taking quite long to execute, is it due to the size of the data?

I have a dataset that is composed of (1069 x 38742), I want to remove all the 3rd columns of the matrix and so I have written a for loop to get this done.

The code is as follows: `

dataTS1 = rand(1069,38742)
for i = 1:12914
  dataTS1(:,3*i) = [];
end`

The problem is that it is taking a very long time to execute this code.

I also tried another following after searching for some other methods such as the following using logical indexing

dataTS1 = rand(1069,38742)
for i = 1:12914
    index = true(1069,size(dataTS1,2));
    index(:,3*i) = false;
    y = dataTS1(:,index);
end

However for the 2nd loop, I get the image error that Index exceeds matrix dimensions.

As for the first loop, I am not sure why it is taking so long.

Upvotes: 0

Views: 39

Answers (1)

Alexander Kemp
Alexander Kemp

Reputation: 202

It takes so long because every time you 'remove' data, you actually copy a large part of your array.

better use (without for loop)

dataTS1(:,3:3:end) = [];

Upvotes: 1

Related Questions