search efficiently in a matrix

2 views (last 30 days)
Mahroo
Mahroo on 8 Aug 2011
I am trying to find some values(F) in a matrix(A) subject to some conditions like what you can see in the code below. now I am using "if" and "for" statements to find the appropriate values, but my matrices are so big and this kind of code searches each row of my matrices which is really time consuming. is there anyway that program can jump on a row which meets a special conditions without testing all rows. It should be noted that the value of F can occure more that once and I need all Fs (finding one F is not enough).
for i = 1:900;
for j =1:900;
....
for k = 1:100;
if matrix_A(k,3) == i
for m = 1:20
if matrix_A(k,1) == matrix_B(m,1)
F = matrix_A(k,j+1)
....
  2 Comments
Oleg Komarov
Oleg Komarov on 8 Aug 2011
You can use logical indexing but it's not clear from your example what you're rtying to accomplish, perhaps if you rephrase or give a complete synthetic example with inputs.
Mahroo
Mahroo on 8 Aug 2011
Thank you. Although the topic of "logical indexing" was not my answer for this question, it was a pretty helpful topic for me as a beginner.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Aug 2011
How do you envision that a computer language might be able to "jump" to a row that meets those conditions without testing the values in the row?
This question is not intended to be rhetorical, but it does require some deep thinking. Historically some computers have implemented this kind of direct access by using a hardware technique called "associative memory". "Associative memory" is still used in some situations: for example it is used in some large multi-cpu computers to handle some shared memory situations. I seem to recall also reading of reference to it with respect to memory management hardware that maps between virtual and physical memory: it would be too time-consuming to search for the right memory descriptor using standard techniques. "Associative memory", in these applications, could be viewed as a form of parallel processing.
There are also software techniques that are very valuable in some situations, primarily situations where a data structure is built once and accessed multiple times. The general technique I am referring to here is known as hashing
  1 Comment
Mahroo
Mahroo on 8 Aug 2011
Thank you for your answer. I do not know how "hashing" can be applied, but if my program's run time goes so high then I need to learn how to use "hashing" functionality.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!