How to Check Within a Range in MATLAB
The math software program MATLAB specializes in calculations involving matrices: rows and columns of numbers. The program's hundreds of built-in functions are what give MATLAB its power. One of those functions is the "find" command, which searches a matrix for the a specific value you need and returns the position or positions in the matrix in which that value resides. The function does not allow the user to check only within a specified range of the matrix, but what you can do instead is first extract just that sub-matrix and then search it for your value.
Instructions
-
-
1
Generate the larger matrix, if you don't already have one. For example, enter this code at MATLAB's command prompt and hit Enter:
A = randi(10,10)
The code creates a 10-row by 10-column matrix of random positive integers no larger than 10 and stories it in a variable "A".
-
2
Extract a submatrix, or sub-range, of "A." For example, if you want to extract only the first half of the matrix, use the following looping code.
for i=1:50
B(i)=A(i);
endThis code assigns the value in the "i-th" position of "A" to the "i-th" position in a new, single-row array "B" for only the first 50 of the 100 values in "A."
-
-
3
Search automatically through the extracted array "B" for the intended value, which simulates checking within the range of the first half of "A" for the value. For example, if you're looking for every time the number three appears, use this code:
find(B==3)
MATLAB will respond with the "i-th" position of every three in "B," which is to say, the "i-th" position of every three in the first half of "A."
-
1