Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does logical indexing work?

Tags:

matlab

In some high-level languages like Matlab, you can use "logical indexing" to select a whole set of entries in an array for operating on.

I understand what logical indexing is and how to use it.

Instead, I am asking:

  • How does it work ("behind the scenes")?
  • Does it not boil down to just a for-loop?
  • If so, why is it so much faster than for-looping?
like image 770
cmo Avatar asked Feb 21 '14 20:02

cmo


People also ask

How are matrices indexed?

Indexing refers to the act of putting an index (or subscript) on a variable assigned to an Array, Matrix, or Vector. For example, if M is a Matrix, then a simple indexing operation is M[1,2], which will extract the element in the first row and second column of M. This can also be acheived using a subscript: .

Does MATLAB count from 0 or 1?

However MATLAB has indexing of arrays beginning from 1 instead of 0, which is the norm in almost every programming languages I have encountered so far.

What is indexing in MATLAB?

Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB® has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs.


1 Answers

Interpreted languages can be thought of as a variation on assembler running on an emulated core. They have stacks and commands that work in ways like the assembler without actually being the assembler. They are a virtual machine.

A for loop can be thought of as telling the system, set a value, run a sequence of tasks, and when you are done then come back and check on that value. If it is not at a threshold, then change it in a prescribed way, and go repeat those tasks and come back. In assembler you are running screaming fast, but in the "VM" not so much. Consider the demonstration between 13:50 and 15:30 of this link: (link)

This means that what appears to be a for loop, isn't actually a for loop. It is operating system interrupts, and virtualized memory. It is virus-scans in the background and megasloth bloatware.

If you had a virtual system, could you make a short-cut for addressing memory that didn't use the virtualized for loop, that was reasonably efficient? MatLab tries to major on data processing, so it has to have very efficient ways of storing, sorting, and selecting data within its virtual machine.

MathWorks is not going to make the details of this accessible to the public. If it has a great idea then they don't want it implemented in Python, and R tomorrow. If it has a mediocre idea then they don't want to be beaten in execution by Python and R tomorrow. Either way, making the nuts and bolts of that particular approach accessible to the public without an NDA - it is likely a losing proposition for them.

Bottom lines:

  • its not a real "for", even for a for loop, because its running virtually
  • they are opening up some of the internals of their data handling to improve usability
  • they aren't likely to disclose actual code because of negative business consequences

It is worthy to note that vectorized code can outperform for loops while doing the same thing. This means they likely are applying more of that internals to execution of the "sequence of tasks" to get performance improvement.

like image 68
EngrStudent Avatar answered Oct 20 '22 06:10

EngrStudent