How does the "find" command work with arrays?

2 views (last 30 days)
So here's my code:
A1 = [1 2; 3 4];
A2 = [0 1; 5 6];
GlobalProd = A1*A2;
IndividProd = A1.*A2;
M1 = find(IndividProd)
M2 = find(GlobalProd)
Here's the output:
M1 =
2
3
4
M2 =
1
2
3
4
I am wondering what values the find command was looking for, since I put the name of an array instead of a specific value?
Thank you.

Accepted Answer

John D'Errico
John D'Errico on 4 Feb 2015
Edited: John D'Errico on 5 Feb 2015
Find looks for NON-zero values. It returns the index of those elements, as they are stored in memory. Remember that elements are stored down columns, so all elements of the first column are first, then the second column.
A linear index is the index of what you would get if you unrolled the array into a long column vector.
If you give find a second return argument, then it returns row and column indices for the non-zeros.
By the way, this is stated in the help, so you could have learned this as easily and more rapidly, simply by reading the help for find. A good way to learn these things is by reading the help, then try some test cases of your own.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!