linear search in matlab

20 views (last 30 days)
javeed miyandad
javeed miyandad on 13 Mar 2011
i have MxN matrix, i need search elements which are more than value 'T' THAT IS . IF 'T'=5 THEN OUTPUT SHOULD BE CO-ORDINATES OF EACH ELEMENT which have value more than or equal to (>=) '5' IN the MxN MATRIX
example let us we have 3X4 matrix
1 2 6 1
4 3 1 0
1 0 5 1
the output should be coordinates of element 6-(3,3), coordinates of element 5-(1,3) plz help in coding
  1 Comment
javeed miyandad
javeed miyandad on 15 Mar 2011
thank you so much i got wat i needed thank you so much sir

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 13 Mar 2011
I won't give you the complete answer, though I'm sure someone else will do so. I want you to learn to experiment in MATLAB, to learn the language, to get a feeling for how it works. This is how to learn such a language. So, try a couple of things. I'll give you enough hints to get there very quickly. First, I'll assume that your matrix is rather uninterestingly called "matrix". Thus
matrix = [1 2 6 1;4 3 1 0;1 0 5 1];
What does MATLAB do when you perform the simple test
T = 5;
result = (matrix >= T)
TRY IT! (Don't just look for me to hand anything to you on a silver platter! I won't do so. This is how you learn.) I put in parens above just for purposes of clarity. They are not needed syntactically here.
The result is a boolean matrix, in MATLAB, an array of type "logical". This array will be of the same size and shape as your original matrix, but composed of zeros and ones. The ones will be in the place of those elements that are greater than T.
Next, what is your goal? You wish to FIND those elements of this boolean variable that are 1, i.e., true. In MATLAB, the basic tool you want is called find. TRY IT. Do something like this now...
result = find(matrix >= T)
What does this return? Gosh, it seems like this should work. In fact, you are almost there. Read the help for find
help find
doc find
You will see that if find is called with TWO output arguments, then it gives you an interesting and perhaps useful result. In MATLAB, you put [ and ] around the arguments when there are two such arguments returned.
The point of all this is to get used to breaking down your problems in MATLAB into small, fundamental units that MATLAB can solve. Look for the utilities in MATLAB that will solve these little problems, and learn to combine them into a solution. The tools to do this are help, doc, and lookfor. And, finally, read the tutorials, the getting started stuff they provide. Had you done so, you would have found all of this VERY quickly, and you would have had your answer very much earlier than you are getting it from me.
  2 Comments
Andrew Newell
Andrew Newell on 13 Mar 2011
John, this is such a nice answer that I voted for the question too.
Paulo Silva
Paulo Silva on 13 Mar 2011
+1 vote also, it's not an answer but a complete tutorial, good work!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!