Find the location of the minimum value of a matrix

Dear all,

I am struggling over a small issue that i am not getting able to find. In fact, i have a matrix M. I am planning to find the location(i,j) of the minimum value of the matrix. For, i wrote a function that is not quite working. Please, find the function below:

proc (1) = finder(aMatrix);
    local row, col, a_vec,minVal, idx, transMat;
    a_vec = vec(aMatrix);
    row = rows(aMatrix);
    col = cols(aMatrix);

    idx = {};

    minVal = minC(a_vec);
    a_vec = (a_vec .== minVal);
    transMat = reshape(a_vec,row,col);

    for i(1,row,1);
        for j(1,col, 1);
            if transMat[i,j] == 1;
                idx = {i,j}; // The problem is coming from here. But, it 
                             //  work if replace{i,j} by {1,1},{0,1) or any values
            endif;
        endfor;
    endfor;

    retp(idx);
endp;

Thanks for your help

1 Answer



0



Problem with your procedure
The problem with this line in your procedure

idx = {i,j};

is that GAUSS, for historical reasons, allows you to put characters inside of a matrix. So the line above is actually embedding the i and j inside of a matrix element.

Solution for this problem

You can resolve this problem by using one of the concatenation operators

// This will create a 2x1 column vector
idx = i | j;

// This will create a 1x2 row vector
idx = i ~ j;

Simpler solution

However, you can create an even simpler solution by using the minindc function. Here is a simple example

new;
// Simple matrix for testing
M = { 1 2 -11  5,
      9 0   2 -8,
      0 1  -9  4 };

// Find the index of the minimum values
{ r, c } = minIdx(M);
print r;
print c;

proc (2) = minIdx(x);
    local r, c;

    // 1. Compute the minimum value for each row
    // 2. Then find the index of the smallest value
    //    from the row minimums
    r = minindc(minc(x'));

    // 1. Compute the minimum value for each column
    // 2. Then find the index of the smallest value
    //    from the column minimums
    c = minindc(minc(x));

    retp(r, c);
endp;

aptech

1,773

Your Answer

1 Answer

0

Problem with your procedure The problem with this line in your procedure

idx = {i,j};

is that GAUSS, for historical reasons, allows you to put characters inside of a matrix. So the line above is actually embedding the i and j inside of a matrix element.

Solution for this problem

You can resolve this problem by using one of the concatenation operators

// This will create a 2x1 column vector
idx = i | j;

// This will create a 1x2 row vector
idx = i ~ j;

Simpler solution

However, you can create an even simpler solution by using the minindc function. Here is a simple example

new;
// Simple matrix for testing
M = { 1 2 -11  5,
      9 0   2 -8,
      0 1  -9  4 };

// Find the index of the minimum values
{ r, c } = minIdx(M);
print r;
print c;

proc (2) = minIdx(x);
    local r, c;

    // 1. Compute the minimum value for each row
    // 2. Then find the index of the smallest value
    //    from the row minimums
    r = minindc(minc(x'));

    // 1. Compute the minimum value for each column
    // 2. Then find the index of the smallest value
    //    from the column minimums
    c = minindc(minc(x));

    retp(r, c);
endp;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.