Finding indices in a magic 5x5 matrix in which the values are greater than 20?

2 views (last 30 days)
There is a 5x5 matrix, and I am trying to find the values that are greater than 20. Would I use something like >>A=magic(5)
A=1 1 1; 1 1 1; 1 1 1; >>[mx,i]=n>20(A) %return values greater than 20
%I don't currently have access to matlab, therefore I'm writing the code on paper ahead of time.

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jun 2014
While the initialization of the magic A matrix is correct, the code that follows is unclear. Why the second initialization of A? What is n?
The easiest way to find those elements of A that are greater than 20 is to use the find function. In the Command Window, type doc find or help find for details on this function. The first example is similar to yours
A = magic(5);
[rIdcs,cIdcs] = find(A>20);
where rIdcs and cIdcs are the row and column indices respectively of those elements of A that are greater than 20.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!