find minimum non-zero

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

find minimum non-zero

Octave - General mailing list
hi, look that:
a=
 1   0   0   8   7   0   0   2
 0   0   0   0   0   0   0   0
 2   0   0   6   0   0   1   0

i want find vector with minimun non-zero


and: 1 (minimun non-zero in  rows n.1)
        0 ( don't find minimum non-zero in  rows n.2)
        2 (minimun non-zero  in  rows n.3)

it's possible to code it avoing loop?





--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: find minimum non-zero

siko1056
On 11/12/19 8:45 AM, shivax via Help list for GNU Octave wrote:

> hi, look that:
> a=
>  1   0   0   8   7   0   0   2
>  0   0   0   0   0   0   0   0
>  2   0   0   6   0   0   1   0
>
> i want find vector with minimun non-zero
>
>
> and: 1 (minimun non-zero in  rows n.1)
>         0 ( don't find minimum non-zero in  rows n.2)
>         2 (minimun non-zero  in  rows n.3)
>
> it's possible to code it avoing loop?
>

Does this work for your problem?

  a = [...
     1   0   0   8   7   0   0   2; ...
     0   0   0   0   0   0   0   0; ...
     2   0   0   6   0   0   1   0];

  a(a == 0) = inf;
  b = min (a, [], 2);
  b(isinf (b)) = 0

I think the answer for the third row should be "1", right?

HTH,
Kai


Reply | Threaded
Open this post in threaded view
|

Re: find minimum non-zero

Octave - General mailing list
skilo1056

correct..
thank you



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: find minimum non-zero

Octave - General mailing list
Sorry
I committed an inaccuracy

i want to find the first number (non zero) in each row


 example:

     1   0   0   8   7   0   0   2
     0   0   0   0   0   0   0   0
     2   0   0   6   0   0   1   0

ans: 1 0 2

another example:

     0   0   0   8   7   0   0   2
     0   0  3   0   0   0   0   0
     5   0   0   6   0   0   1   0

ans: 8 3 5


 




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: find minimum non-zero

mmuetzel
Am 14. November 2019 um 16:48 Uhr schrieb "gigiolone123 via Help list for GNU Octave":

> Sorry
> I committed an inaccuracy
>
> i want to find the first number (non zero) in each row
>
>
>  example:
>
>      1   0   0   8   7   0   0   2
>      0   0   0   0   0   0   0   0
>      2   0   0   6   0   0   1   0
>
> ans: 1 0 2
>
> another example:
>
>      0   0   0   8   7   0   0   2
>      0   0  3   0   0   0   0   0
>      5   0   0   6   0   0   1   0
>
> ans: 8 3 5
>

There are probably several possible solutions. One involves meshgrid, logical indexing and min.
But you should do your homework on your own.

Markus