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:
Condensing a vector

Subject: Condensing a vector

From: Yoav Rubin

Date: 9 Apr, 2009 04:43:01

Message: 1 of 9

Hi All,

I have a vector (A) with n items, and I'd like to build a new vector (B) based on the old vector, that has m items, where m > n. I need the values of the items in B to be a "condensed" version of the values in A. For example, if A = (0,2,10) and m=5 , then B will be (0,1,2,6,10). Basically the idea is to somehow "plot" A, then set the X values to be 1:1:m and B is the Y values of that new "plot".

thanks

Yoav

Subject: Condensing a vector

From: Darren Rowland

Date: 9 Apr, 2009 05:31:01

Message: 2 of 9


Yoav,
Your request is very vague. I would consider B to be an "expanded" version of A, not a "condensed" version. That being said, how do you decide where to put the new values. Will there always be m=n-1 new values which slot evenly between the old values? Please explain further.
Darren

Subject: Condensing a vector

From: Roger Stafford

Date: 9 Apr, 2009 05:35:02

Message: 3 of 9

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <grjucl$mp1$1@fred.mathworks.com>...
> Hi All,
>
> I have a vector (A) with n items, and I'd like to build a new vector (B) based on the old vector, that has m items, where m > n. I need the values of the items in B to be a "condensed" version of the values in A. For example, if A = (0,2,10) and m=5 , then B will be (0,1,2,6,10). Basically the idea is to somehow "plot" A, then set the X values to be 1:1:m and B is the Y values of that new "plot".
>
> thanks
>
> Yoav

  If you are doing this to get a better-looking plot, I would think that using the matlab function 'interp1' would be preferable for you. That would allow you to define m equally-spaced points for X which give the interpolated values Y obtained from A. Then you could just plot X against Y. Or you could combine the sets of points in an appropriate manner and plot them.

  The way you have it set up now, you are, in effect, interpolating linear segments between the points of A, which may not make as desirable a plot.

Roger Stafford

Subject: Condensing a vector

From: Yoav Rubin

Date: 9 Apr, 2009 06:03:07

Message: 4 of 9

"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <grk16l$opp$1@fred.mathworks.com>...
>
> Yoav,
> Your request is very vague. I would consider B to be an "expanded" version of A, not a "condensed" version. That being said, how do you decide where to put the new values. Will there always be m=n-1 new values which slot evenly between the old values? Please explain further.
> Darren

Darren,

I cannot define beforehand the relationship between m and n. Basically I have a source vector with n values and a number m that defines the number of elements in a new vector, and these values should be deduced from the source values in a way, that if you would visualize both the source and the target vector, their plot would look the same (ignoring the differences on the x axis) - is this explanation clearer?
As for expanded vs condensed - I called it condensing as m > n and in therefore there would be more points between the two edge values of A(1) and A(numel(A))

thanks

Yoav

Subject: Condensing a vector

From: Darren Rowland

Date: 9 Apr, 2009 06:47:01

Message: 5 of 9


> I cannot define beforehand the relationship between m and n. Basically I have a source vector with n values and a number m that defines the number of elements in a new vector, and these values should be deduced from the source values in a way, that if you would visualize both the source and the target vector, their plot would look the same (ignoring the differences on the x axis) - is this explanation clearer?

Yoav,
I agree with Roger's sentiment that you are probably not taking the correct approach to your problem. Maybe to clear things up you can provide an answer to these queries.
If A = (0,3,6) and m=5, will the answer be
B = (0, 1, 2, 3, 6),
B = (0, 1.5, 3, 4.5, 6) or
B = (0, 3, 4, 5, 6) ? And why?

What would the answer be when m=6?
Darren

Subject: Condensing a vector

From: Bruno Luong

Date: 9 Apr, 2009 07:13:02

Message: 6 of 9

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <grjucl$mp1$1@fred.mathworks.com>...
> Hi All,
>
> I have a vector (A) with n items, and I'd like to build a new vector (B) based on the old vector, that has m items, where m > n. I need the values of the items in B to be a "condensed" version of the values in A. For example, if A = (0,2,10) and m=5 , then B will be (0,1,2,6,10). Basically the idea is to somehow "plot" A, then set the X values to be 1:1:m and B is the Y values of that new "plot".
>
> thanks
>
> Yoav

step=1/2 % <- 1/2, 1/4, 1/8 etc...
B = interp1((0:length(A)-1),A,(0:step:length(A)-1))

Bruno

Subject: Condensing a vector

From: Yoav Rubin

Date: 9 Apr, 2009 07:20:02

Message: 7 of 9

Darren,
B = (0, 1.5, 3, 4.5, 6) is what I am looking for.
Basically A is an output of a sensor, and I need to create from that output the output that that sensor would have created if it had m points of sampling. So assuming that the values in A were taken in equaly spaced intervals, I need B to represent that same output (which is real data) given that there were m points of samping and not n (where n=numel(A))

thanks

"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <grk5l5$ah8$1@fred.mathworks.com>...
>
> > I cannot define beforehand the relationship between m and n. Basically I have a source vector with n values and a number m that defines the number of elements in a new vector, and these values should be deduced from the source values in a way, that if you would visualize both the source and the target vector, their plot would look the same (ignoring the differences on the x axis) - is this explanation clearer?
>
> Yoav,
> I agree with Roger's sentiment that you are probably not taking the correct approach to your problem. Maybe to clear things up you can provide an answer to these queries.
> If A = (0,3,6) and m=5, will the answer be
> B = (0, 1, 2, 3, 6),
> B = (0, 1.5, 3, 4.5, 6) or
> B = (0, 3, 4, 5, 6) ? And why?
>
> What would the answer be when m=6?
> Darren

Subject: Condensing a vector

From: Bruno Luong

Date: 9 Apr, 2009 08:01:04

Message: 8 of 9

I'm not sure you have seen it, so I post again.

step=1/2 % <- 1/2, 1/4, 1/8 etc... inverse of the "density"
B = interp1((0:length(A)-1),A,(0:step:length(A)-1))

Bruno

Subject: Condensing a vector

From: Yoav Rubin

Date: 9 Apr, 2009 08:29:01

Message: 9 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <grka00$nav$1@fred.mathworks.com>...
> I'm not sure you have seen it, so I post again.
>
> step=1/2 % <- 1/2, 1/4, 1/8 etc... inverse of the "density"
> B = interp1((0:length(A)-1),A,(0:step:length(A)-1))
>
> Bruno

Thanks Bruno, that did what I needed

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