COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Finding the two closest array values to some arbitrary value

• Follow

```I would like to identify the row indices of two minimum values in a cell array, containing data Zi. Searching for a single value is simple (see below) but how to I search for the minimum and the closest value to that minimum?

minimum value search:
c=find(min(Z)==Z); %find indices
```
 0
Reply Daniel 11/17/2009 2:50:06 PM

```"Daniel " <elefant2_2000@yahoo.de> wrote in message <hdud6u\$dpn\$1@fred.mathworks.com>...
> I would like to identify the row indices of two minimum values in a cell array, containing data Zi. Searching for a single value is simple (see below) but how to I search for the minimum and the closest value to that minimum?
>
> minimum value search:
>             c=find(min(Z)==Z); %find indices

copyZ = Z;
c =find(min(Z)==Z);
copyZ(c) = NaN;
c2=find(min(copyZ)==copyZ);

or
s = unique(sort(Z));
c =find(s(1)==Z);
c2= find(s(2)==Z);
```
 0
Reply Matthew 11/17/2009 6:57:03 PM

```"Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hdurlv\$372\$1@fred.mathworks.com>...
> "Daniel " <elefant2_2000@yahoo.de> wrote in message <hdud6u\$dpn\$1@fred.mathworks.com>...
> > I would like to identify the row indices of two minimum values in a cell array, containing data Zi. Searching for a single value is simple (see below) but how to I search for the minimum and the closest value to that minimum?
> >
> > minimum value search:
> >             c=find(min(Z)==Z); %find indices
>
> copyZ = Z;
> c =find(min(Z)==Z);
> copyZ(c) = NaN;
> c2=find(min(copyZ)==copyZ);
>
> or
> s = unique(sort(Z));
> c =find(s(1)==Z);
> c2= find(s(2)==Z);

Of course if you don't need the indices separated the 2nd solution could be written as:
s = unique(sort(Z));
c =find(Z <= s(2));
```
 0
Reply Matthew 11/17/2009 7:43:03 PM

2 Replies
562 Views

(page loaded in 0.047 seconds)

Similiar Articles:

7/24/2012 5:04:50 AM