How Can I Vectorize Nested For-Loops in Matlab for Handwriting Recognition?

  • Thread starter sjaakiejj
  • Start date
In summary, the conversation is about the speaker's experience with using Matlab for a handwriting recognition project. They mention that Matlab is not efficient with for-loops and they understand the basics of vectorization but are unsure how to apply it to their code. They also mention using a k-Nearest-Neighbor classifier and ask for advice on how to vectorize their code.
  • #1
sjaakiejj
1
0
Hi,

I've been a programmer in Java and C++ for about 4 years now, and recently started using Matlab for a small project for handwriting recognition.
Matlab however is terribly inefficient when it comes to for-loops, making the code I currently use horribly slow, having to wait for minutes before the results come forward. I understand the basics of vectorization in code, using built-in commands such as sum and ./ .* and .^, however none of these seem to provide me with what I want.

Note that vectorizing just one of the two for-loops would already make a huge difference, as it would mean it iterates 89700 times less.

Data is a dataset of images represented as vectors (reshaped matrices with pixel information), and labels is what identifies these data samples to the digit they represent.

Code:
wrong = zeros(300,1);
numberOfSamples = 300;

for k = 1:numberOfSamples
  for i = 1:numberOfSamples
      wrong(k) = wrong(k) + (knearest(k, data(i,:), data, labels) ~= labels(i,1));
  end;
end;


Knearest is the k-Nearest-Neighbour classifier, which classifies a datasample to a certain class. So my question here is, how do I vectorize this code, if it's possible at all? I'd imagine something's possible with k, where as i would become much harder.

Thanks in advance.
 
Physics news on Phys.org
  • #2
Well, that depends on how knearest handles matrices:

Code:
N = 300;
wrong = zeros(N,1);

for k = 1:N
      wrong(k) = wrong(k) + (knearest(k, data, data, labels) ~= labels(i,1));
end
 

Related to How Can I Vectorize Nested For-Loops in Matlab for Handwriting Recognition?

What is vectorization?

Vectorization is the process of converting a for-loop, which processes data element by element, into a vectorized operation, which processes a whole array of data at once. This can improve the efficiency and speed of code execution.

Why is vectorization important?

Vectorization is important because it can significantly improve the performance of code, especially when working with large datasets. It also allows for easier implementation of mathematical and scientific operations, making code more concise and readable.

How can I vectorize a for-loop?

To vectorize a for-loop, you can use built-in functions and operations that are optimized for vectorized operations, such as NumPy in Python or apply functions in R. You can also use parallel processing techniques to further enhance the performance of vectorized code.

What are the benefits of vectorization?

Besides improving code efficiency and speed, vectorization also reduces the need for manual coding and debugging, making the development process more efficient. It also allows for easier scalability, as vectorized operations can easily handle larger datasets without significant changes to the code.

Are there any drawbacks to vectorization?

One potential drawback of vectorization is that it may require a certain level of expertise and knowledge of specific tools and libraries. It may also not be suitable for all types of operations and may not always result in significant performance gains. Additionally, vectorization may also increase memory usage, so it's important to consider memory limitations when vectorizing code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
603
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
Back
Top