bubbles
bubbles

Reputation: 881

Efficient Access of elements in Matrix in Matlab

I have an m x n matrix of integers and where n is a fairly big number m and n ~1000. I want to iterate through all of these and perform a some operations, like accessing a particular cell and assigning a value of a particular cells.

However, at least in my implementation, this is rather inefficient as I have two for loops with Matrix(a,b) = Matrix(a,b+1) or something along these lines. Is there any other way to do this seeing as my current implementation takes a long time to traverse through about 100,000 cells and perform some operations.

Thank you

Upvotes: 1

Views: 191

Answers (3)

James
James

Reputation: 3231

If you can't get rid of the for loops, one possibility would be to rewrite the expensive operations in C and create a MEX file as described here.

Upvotes: 0

Oli
Oli

Reputation: 16035

In matlab, it's almost always possible to avoid loops. If you want to do Matrix(a,b)=Matrix(a,b+1), you should just do Matrix2=Matrix(:,2:end);

If you are more precise about what you do inside the loop, I can help you more.

Upvotes: 1

Niclas
Niclas

Reputation: 1230

Matlab uses column major ordering of matrixes in memory (unlike C). Are you sure you are iterating the indexes in the correct order? If not, try switching them and see if performance improves..

Upvotes: 0

Related Questions