Message Boards Message Boards

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


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.

10 months ago

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

POSTED BY: Hans Dolhaine
10 months ago

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)

POSTED BY: John Hendrickson
10 months ago

Group Abstract Group Abstract