Link to home
Start Free TrialLog in
Avatar of peiyoke
peiyoke

asked on

Comparing array

Hi,

I have a 2-d array, a[i][j]  in which I need to compare with another 1-d array, b[k]. For each element in the 2-d array, I compare it with every element in the 1-d array. In the comparison, I find the element in b[k] which gives the minimum value of a[i][j]-b[k] and assign the value of b[k] to a[i][j].


What I did was :

Change the 2-d array to 1-d array
Find the difference between each of the new 1-d array and b[k] and store it in a temporary 2-d array.
Find the minimum value in the temporary 2-d array.
Assign value of b[k] which gives the minimum value in temporary 2-d array to the new 1-d array.
Change the new 1-d array back to 2-d array.

I find this very inefficient. How can I implement it in a more efficient way?

regards,
PY
Avatar of newexpert
newexpert

Initial thought: sort array b[k] first.  Then use bsearch() for each element of a[i][j]
I mean binary search the closest b[k] element to each a[i][j]
Avatar of ozo
Depending on the sizes of the arrays, you may also want to sort a[i][j]
But otherwise, I see no purpose in converting the 2-d array to 1-d array.

Do you really mean minimum a[i][j]-b[k] or minimum abs(a[i][j]-b[k])?
If the former, you'd just take the maximum b[k] for all a
Avatar of peiyoke

ASKER

There is no way to sort b[k] because it is not a single value. When I said that I find the (a[i][j]-b[k]), it is just a representation of what I am doing.

I convert the 2-d array to 1-d array so that later on in the temporary 2-d array, 1 dimension can be used to keep track of the elements from the a[i][j] and another dimension to keep track of elements from b[k].

Is there another way of doing this?
If you give us just a representation of what you are doing, then all we can give you is a representation of an answer.
Can you explain any more about what you're trying to do?
Not being a single value may not be an obstacle to sorting, but
sorting may not be appropriat of all possible representations of things you may be doing.
At worse, it seems you could just look at each element of a[i][j] in turn,
and evaluate (a[i][j]-b[k]) for all k, keeping track of the minmimum with still no need for any auxilliary array,
Or depending on what (a[i][j]-b[k]) really represets, there may be a way of saving some of the effort of multiple evaluations, but it's hard to guess whether this may be possble without furthur information.
Avatar of peiyoke

ASKER

The array a[i][j] and b[k] are both structure with a few members. A formula is applied to find the most similar element in b[k] which can represent each element in a[i][j]. After obtaining the most similar element, each value of a[i][j] will be substituted by the value of b[k].

In other words, I am trying to classify the elements in a[i][j] into b[k]. After classifying the elements into their respective groups, I need to code the elements using the index of b[k]. Then, I need to decode it again.
ASKER CERTIFIED SOLUTION
Avatar of onki
onki

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial