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:
averaging vectors of different dimensions

Subject: averaging vectors of different dimensions

From: perfreem@gmail.com

Date: 19 Jan, 2009 23:24:38

Message: 1 of 4

hello,

i am tryin to average together several columns of different matrices,
each column of slightly different sizes (difference might be one or
two elements).
for example, my columns a, b, c might be:
  a = my_mat1(:, 2); # size 10
  b = my_mat2(:, 2); # size 12
  c = my_mat3(:, 2); # size 11

i want to make a new vector d that contains the averages of all their
values. the first 10 values can be averaged, then for the 11th i want
to have the average of b and c's values, and for the 12 value i only
want c's value.

is there an easy way to do this? a simple: a' + b' + c' ./ 3 does not
work because a b and c are all different sizes....

    thank you.

Subject: averaging vectors of different dimensions

From: Sadik

Date: 20 Jan, 2009 00:10:04

Message: 2 of 4

perfreem@gmail.com wrote in message <d54e80af-5e1c-4601-9f1a-b7742ffb268a@g38g2000yqn.googlegroups.com>...
> hello,
>
> i am tryin to average together several columns of different matrices,
> each column of slightly different sizes (difference might be one or
> two elements).
> for example, my columns a, b, c might be:
> a = my_mat1(:, 2); # size 10
> b = my_mat2(:, 2); # size 12
> c = my_mat3(:, 2); # size 11
>
> i want to make a new vector d that contains the averages of all their
> values. the first 10 values can be averaged, then for the 11th i want
> to have the average of b and c's values, and for the 12 value i only
> want c's value.
>
> is there an easy way to do this? a simple: a' + b' + c' ./ 3 does not
> work because a b and c are all different sizes....
>
> thank you.

How about this one [I believe in the text you have written c instead of b, because in the code, it seems that it is b whose length is 12]:

([a;0;0]+b+[c;0])./[3*ones(length(a),1);2;1]

Subject: averaging vectors of different dimensions

From: perfreem@gmail.com

Date: 20 Jan, 2009 03:15:21

Message: 3 of 4

On Jan 19, 7:10=A0pm, "Sadik " <sadik.h...@gmail.com> wrote:
> perfr...@gmail.com wrote in message <d54e80af-5e1c-4601-9f1a-b7742ffb2...=
@g38g2000yqn.googlegroups.com>...
> > hello,
>
> > i am tryin to average together several columns of different matrices,
> > each column of slightly different sizes (difference might be one or
> > two elements).
> > for example, my columns a, b, c might be:
> > =A0 a =3D my_mat1(:, 2); # size 10
> > =A0 b =3D my_mat2(:, 2); # size 12
> > =A0 c =3D my_mat3(:, 2); # size 11
>
> > i want to make a new vector d that contains the averages of all their
> > values. the first 10 values can be averaged, then for the 11th i want
> > to have the average of b and c's values, and for the 12 value i only
> > want c's value.
>
> > is there an easy way to do this? a simple: a' + b' + c' ./ 3 does not
> > work because a b and c are all different sizes....
>
> > =A0 =A0 thank you.
>
> How about this one [I believe in the text you have written c instead of b=
, because in the code, it seems that it is b whose length is 12]:
>
> ([a;0;0]+b+[c;0])./[3*ones(length(a),1);2;1]

hi sadik,

thanks for your reply.

i am talking about the general case here though, the a, b, c was just
an example. i want to do this for n-many different vectors of all
potentially slightly different sizes...

Subject: averaging vectors of different dimensions

From: Bruno Luong

Date: 20 Jan, 2009 06:52:01

Message: 4 of 4

% Data
a=rand(10,1);
b=rand(12,1);
c=rand(11,1);
v={a b c};

% Algo
m=max(cellfun(@length,v));
n=length(v);
V=zeros(m,n);
mask=zeros(m,n);
for i=1:length(v)
    vi=v{i};
    V(1:length(vi),i)=vi;
    mask(1:length(vi),i)=ones(size(vi));
end

MeanV = sum(V.*mask,2)./sum(mask,2)

% Bruno

Tags for this Thread

No tags are associated with 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