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:
vector of minimum

Subject: vector of minimum

From: Luca Feliciani

Date: 10 Nov, 2010 16:51:04

Message: 1 of 9

Hi!
I have a vector and a variable and I need a vector that gives me the nearest elements of the vector, to the variable.
I have a vector
   a=[300;470;680;770;660;570;370;290;300;460]
and a variable
   b=400
I use this expression
   [min_difference, array_position] = min(abs(a - b))
and I obtain the element with minimum distance, but i need a list of nearest elements, not only one.
There is a method that permit to avoid nested cycles?
Thanks!

Subject: vector of minimum

From: Sean

Date: 10 Nov, 2010 17:00:07

Message: 2 of 9

"Luca Feliciani" <lookaffe@libero.it> wrote in message <ibeiho$gv7$1@fred.mathworks.com>...
> Hi!
> I have a vector and a variable and I need a vector that gives me the nearest elements of the vector, to the variable.
> I have a vector
> a=[300;470;680;770;660;570;370;290;300;460]
> and a variable
> b=400
> I use this expression
> [min_difference, array_position] = min(abs(a - b))
> and I obtain the element with minimum distance, but i need a list of nearest elements, not only one.
> There is a method that permit to avoid nested cycles?
> Thanks

min_els = sort(abs(a-b));
min_els(1:number_of_close_els_you_want);

Subject: vector of minimum

From: DitzaN

Date: 10 Nov, 2010 17:19:49

Message: 3 of 9

Hi,

what do you mean by list of nearest elements?
what would you like yours answer to be?

maybe sort abs(a - b) will give you what you are looking for

Subject: vector of minimum

From: Luca Feliciani

Date: 10 Nov, 2010 19:36:03

Message: 4 of 9

I tried the "sort" solution but my problem is a little more complicated.
Maybe i can explain in a different way.
I have a matrix (not a vector)
  a=
300 2300 3070 3590
470 2180 2720 3790
680 1890 2580 3940
770 1400 2460 3710
660 1170 2770 3650
570 840 2640 3310
370 730 2670 3240
290 700 2550 3280
300 1890 2250 3000

and a vector b=[358;782;2443;3336]
I have to find the row of the matrix that best matches my vector taking in account the hieararchy of the vector (the first element is the most important), then i search for the "nearest" with the first column, select few that fall in the range, and proceed with second elements and second column. As result i need the index of the row, cause i have another array with different information.

i have a solution that can be optimized:

range=[70 100 200]
for i=1:length(range)
    asrc=abs(a(:,i) - b(i)); % difference
    [asrc, isrc]=sort(asrc); % sort with index
    asrc=asrc(asrc<range(i)); % check range
    isrc=isrc(1:length(asrc)); % select the nearest
    a=a(isrc,:); %take the nearest
end

some advice?
THANKS!

Subject: vector of minimum

From: Bruno Luong

Date: 10 Nov, 2010 20:01:05

Message: 5 of 9

[~, loc] = sortrows(abs(bsxfun(@minus,a,b(:)')));
loc = loc(1)

a(loc,:)
b(:)'

Bruno

Subject: vector of minimum

From: Luca Feliciani

Date: 11 Nov, 2010 16:18:03

Message: 6 of 9

Thanks bruno, I've learned something new, but the result is different from what I expect. This code select the elements only comparing the first column. I have to select a set of elements comparing the first column of a (with the first element of b), then another set comparing the second row of the selected set (with the second element of b), until i obtain a single row from the matrix a...

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibetm1$3h0$1@fred.mathworks.com>...
> [~, loc] = sortrows(abs(bsxfun(@minus,a,b(:)')));
> loc = loc(1)
>
> a(loc,:)
> b(:)'
>
> Bruno

Subject: vector of minimum

From: Bruno Luong

Date: 11 Nov, 2010 16:48:05

Message: 7 of 9

"Luca Feliciani" <lookaffe@libero.it> wrote in message <ibh4vr$91v$1@fred.mathworks.com>...
> Thanks bruno, I've learned something new, but the result is different from what I expect. This code select the elements only comparing the first column.

No it's not true, if there is many draw distances of the first column, it look for the the best second column, etc ...

If you want some sort of tolerance, just round (categorize) the differential values at your need. I let you adapt this part.

>I have to select a set of elements comparing the first column of a (with the first element of b), then another set comparing the second row of the selected set (with the second element of b), until i obtain a single row from the matrix a...
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibetm1$3h0$1@fred.mathworks.com>...
> > [~, loc] = sortrows(abs(bsxfun(@minus,a,b(:)')));
> > loc = loc(1)
> >
> > a(loc,:)
> > b(:)'
> >
> > Bruno

Subject: vector of minimum

From: Bruno Luong

Date: 11 Nov, 2010 18:36:04

Message: 8 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibh6o5$5sn$1@fred.mathworks.com>...

>
> If you want some sort of tolerance, just round (categorize) the differential values at your need. I let you adapt this part.

Well, I'll do it this adaptation as well

a=[ 300 2300 3070 3590;
    470 2180 2720 3790;
    680 1890 2580 3940;
    770 1400 2460 3710;
    660 1170 2770 3650;
    570 840 2640 3310;
    370 730 2670 3240;
    290 700 2550 3280;
    300 1890 2250 3000 ];

b=[358 782 2443 3336];

range=[70 100 200 1000]

d = abs(bsxfun(@minus,a,b));
outrange = bsxfun(@ge, d, range)
[~, loc] = sortrows(outrange);
loc = loc(1)

b
a(loc,:)

Bruno

Subject: vector of minimum

From: Luca Feliciani

Date: 12 Nov, 2010 11:04:07

Message: 9 of 9

Perfect!
I did not understand how to best use sortrows!
Thanks
Luca

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