Dan S. answered 10/18/21
Data scientist and machine learning engineer
This problem can be solved multiple ways. I assume since it specifically asks us to use loops, it is presented here to practice writing loops in MATLAB. I'll show how this can be done. However, I'll also add some additional information about better ways to solve this problem and why you would want to do it that way in practice.
=====
Surface Level Solution
To solve the problem as presented, first define the matrix M.
We can use nested for loops to iterate over both the rows and columns of the matrix M. First, we'll need to know how many rows and columns it has. We'll also want to prepare some variables that can store the requested information:
- count is a scalar that counts how many entries are greater than 1. It starts out as 0.
- row is a vector that stores all the row indices from the entries greater than 1. It starts out as an empty array.
- col is a vector that stores all the column indices from the entries greater than 1. It starts out as an empty array.
Now we are ready to build our loop. We look through every row (using a for-loop). Inside every row, we look through every column (using a for-loop). This allows us to traverse every element of the matrix. For every element, we check if the value is greater than 1 (using an if-statement). Then, when that check is true, we update our three variables. Increment the count. Then extend our lists of row and column indices by concatenating the new indices onto the existing list of indices.
=====
Deeper Dive
The above code is a complete solution to the question that was presented. However, in practice this code is actually quite inefficient. "MATLAB" stands for "MATrix LABoratory," and much of its power comes from its ability to do matrix and vector operations very efficiently due to the math libraries it's built on top of. Using for loops is generally less efficient than writing vectorized code. And the above solution is further slowed down by iteratively growing an array inside the loop.
Vectorization refers to writing code that applies to an entire variable all at once rather than looping through its elements. In addition to running faster, this code can also be simpler to write. In fact, we can solve the entire problem above using only two lines of code as follows:
Let's examine what's happening here. First, we are performing the logical operation M>1. The result of this expression is a logical array the same size as the original matrix. In this case, it becomes a 2-by-2 matrix of true and false values that's true everywhere M has a value greater than 1. One convenience for us is that if we sum a logical array, it gives us a count of how many true values it contains. Therefore, the first line of code here implicitly creates an array that's true for all the entries we care about and then counts the number of true entries.
What about the second line of code above? Well it turns out that MATLAB has other convenience functions that operate on logical arrays. The find function allows us to find the row and column positions of all the true elements in an array. Recall that the logical array produced by the expression "M>1" has true elements everywhere that M has a value greater than 1. Thus, this statement gives us the row and column positions for all those entries.
Finally, it's worth noting that for many applications, it's not even necessary to extract the row and column positions that you're interested in. Most of the time you want to know the positions of certain elements, it's because you want to do something to those elements such as extract them or change them. For most of the data manipulation you do in MATLAB, you can use what's called a "logical index" to specify the location of the data you want to change. For example, here's a list of things you can do with the data in M that is greater than 1:
In each of these operations, you don't need to store the row and column positions of the data you're interested in. Instead, you use the logical expression M>0 to tell MATLAB how to find the data you're interested in and what to do with it.