From: Aman on
i have this matrix and i m able to fine the maximum value in each row. But when i am using function min it gives the answer 0 which is correct. But want to find the minimum value which is greater then 0 in each row.(i.e- in row 1 & 2 its 2,in row 3 its 4,in row 4 & 5 its 1 and in row 6 its 9)
please help.

G_k=[0,2,11,0,5,0;2,0,0,6,0,0;11,0,0,4,7,0;0,6,4,0,1,9;5,0,7,1,0,12;0,0,0,9,12,0]
G_k=sparse(G_k');
MaxG_k=max(G_k)
From: Walter Roberson on
Aman wrote:
> i have this matrix and i m able to fine the maximum value in each row.
> But when i am using function min it gives the answer 0 which is correct.
> But want to find the minimum value which is greater then 0 in each
> row.(i.e- in row 1 & 2 its 2,in row 3 its 4,in row 4 & 5 its 1 and in
> row 6 its 9)
> please help.
> G_k=[0,2,11,0,5,0;2,0,0,6,0,0;11,0,0,4,7,0;0,6,4,0,1,9;5,0,7,1,0,12;0,0,0,9,12,0]
>
> G_k=sparse(G_k');
> MaxG_k=max(G_k)

There is more than one technique for this. I seem to recall reading that
there is a way to do it that is specific to sparse matrices (such as you
construct), but I am not sufficiently acquainted with sparse matrices to
know that technique.

For reasonable sized non-sparse matrices, you can use,

t = G_k;
t(t<=0) = inf;
MinG_k = min(t);

Note that this is applicable to finding the _minimum_ value such as you
describe in your question, where-as your sample code was to find
_maximum_ values; for maximum values, use -inf instead of inf.

This would *not* be a good technique for large sparse matrices!
From: Bruno Luong on
Walter Roberson <roberson(a)hushmail.com> wrote in message <_8FYn.6119$Zi.980(a)newsfe14.iad>...

>
> This would *not* be a good technique for large sparse matrices!

For sparse, use FIND/ACCUMARRAY

[i j s] = find(G_k)
% max of non-zeros elements along column
accumarray(j,s,[],@max)
max(G_k)

% min of non-zeros elements along column
accumarray(j,s,[],@min)

% Bruno