# Pick the elements of a matrix in increasing numerical values for loop op?

**URL:** <https://community.wolfram.com/t/pick-the-elements-of-a-matrix-in-increasing-numerical-values-for-loop-op/11307>\
**Category:** Questions\
**Tags:** logic\
**Created:** [September 22, 2017, 6:43pm UTC](https://community.wolfram.com/t/pick-the-elements-of-a-matrix-in-increasing-numerical-values-for-loop-op/11307 "2017-09-22T18:43:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shg](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@shg](https://community.wolfram.com/u/shg)\
**Post date:** [September 22, 2017, 6:43pm UTC](https://community.wolfram.com/t/pick-the-elements-of-a-matrix-in-increasing-numerical-values-for-loop-op/11307/1 "2017-09-22T18:43:24Z")

</div>

Dear Friends,  
I have a programming problem which seems to be a case for If/For functions but I am not able to formulate it properly. Attached file contains a simplified script which is my first thought, but it can work only if somehow I find a way to pick the elements of a matrix in increasing numerical values. I could not think of a way out, will appreciate any suggestion on it. Following is the short description of the problem. Thanks

I have two 6 X 6 matrix G and A having elements to be a function of integer m having values 0 - 5. At the beginning of loop m = 0.

```
G[m_] = {{g11, g12, g13, g14, g15 g16}, ., {g21, g22, g23, g56, g66}}

A[m_] = {{a11, a12, a13, a14, a15 a16}, ., {a21, a22, a23, a56, a66}}

```

There is another six elements row matrix

```
H[m_] = {h1, h2, h3, h4, h5, h6}

```

Now I want to perform a loop operation on the system such that elements of matrix G in the order of increasing numerical values are compared with the corresponding elements of A such that if for the element [i, j]

If g[i j] \< A[i, j] → next higher magnitude g[i, j] is selected

Or if  
g[i j] \> A[i, j] → m increases by 1 (m++) and hence H[m] → H[m++], where

```
H[m++] = ReplacePart[H[m], {j-> hj+hi, i-> 0]

```

Now for new value of m, G and A will have new elements and cycle will continue till m = 5, and at this point one element of H[5] matrix would be h1+h2+h3+h4+h5+h6 with all others to be zero.

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [September 26, 2017, 8:57pm UTC](https://community.wolfram.com/t/pick-the-elements-of-a-matrix-in-increasing-numerical-values-for-loop-op/11307/2 "2017-09-26T20:57:08Z")

</div>

I do not understand what you really want to do. I attach a notebook that does “something” to H by comparing matrix-elements. But you will notice in the printout that your condition G[i,j] \> A[i,j] is often matched for low values of i and j, so higher elements of H are left alone.

Further you can see the condition often is fulfilled for i = j , so {j-\> hj+hi, i-\> 0 translates to j → j + i = 2 i and j → 0. Is it this what you want?

Perhaps you could describe your problem a bit more lucid, or write down the actions you think of “by hand”.

Regards HD

---

<div class="post-metadata">

**Author:** ![anon\_91240867](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anon\_91240867](https://community.wolfram.com/u/anon_91240867)\
**Post date:** [September 28, 2017, 9:52pm UTC](https://community.wolfram.com/t/pick-the-elements-of-a-matrix-in-increasing-numerical-values-for-loop-op/11307/3 "2017-09-28T21:52:55Z")

</div>

I might try Sort[], and it does allow a function to decide order (thus, you could compare another matrix to determine the ordering given arguments)
