Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using min to find non-zero minimum

Subject: using min to find non-zero minimum

From: Natalie Sin Hwee

Date: 19 Jun, 2010 18:24:08

Message: 1 of 5

Dear Mathwork users

I have a 1000x1000 matrix with values between 0-1. How can i find the non-zero minimum from each column?

I've tried using min but that gives me all zeros

Thanks
Natalie^^

Subject: using min to find non-zero minimum

From: Matt Fig

Date: 19 Jun, 2010 18:34:05

Message: 2 of 5

One approach:


% Example data:
A = round(rand(6,6)*3)

% Get the non-zero mins
A(~A) = nan;
col_mins = min(A)

Subject: using min to find non-zero minimum

From: Natalie Sin Hwee

Date: 19 Jun, 2010 18:58:04

Message: 3 of 5

"Matt Fig" <spamanon@yahoo.com> wrote in message <hvj2it$irj$1@fred.mathworks.com>...
> One approach:
>
>
> % Example data:
> A = round(rand(6,6)*3)
>
> % Get the non-zero mins
> A(~A) = nan;
> col_mins = min(A)

Dear Matt,

that worked perfectly!! just what i wanted!!

Thank you!!
Natalie

Subject: using min to find non-zero minimum

From: Matt J

Date: 19 Jun, 2010 19:27:04

Message: 4 of 5


> % Get the non-zero mins
> A(~A) = nan;
> col_mins = min(A)

I've been advised in past threads that NaN processing is a lot slower than Infs. It would be advisable to use

A(~A) = inf;

instead.

Subject: using min to find non-zero minimum

From: Matt Fig

Date: 19 Jun, 2010 21:36:06

Message: 5 of 5

Wow, you weren't joking Matt! As far as the writing is concerned, there is not much difference. But for MATLAB to deal with the array, BIG DIFFERENCE.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = nan_inf()
% Is dealing with infs faster than writing nans?
t = 0;

for ii = 1:100
    A = round(rand(1000));
    tic
    A(~A) = nan;
    colmins = min(A);
    t = t + toc;
end

fprintf('Time for the nans is: %.3f seconds.\n',t)
t = 0;

for ii = 1:100
    A = round(rand(1000));
    tic
    A(~A) = inf;
    colmins = min(A);
    t = t + toc;
end

fprintf('Time for the infs is: %.3f seconds.\n',t)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





>> nan_inf
Time for the nans is: 16.497 seconds.
Time for the infs is: 4.969 seconds.
>> nan_inf
Time for the nans is: 16.300 seconds.
Time for the infs is: 4.951 seconds.
>>

On winvista 32 using 2007b and an old laptop.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us