Thread Subject: Create a new vector from a current vector

Subject: Create a new vector from a current vector

From: Amir Safari

Date: 5 Nov, 2007 15:04:45

Message: 1 of 8


Hi MATLAB users,

Suppose we have a matrix or vector with only one column of
length 20.

A=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];

My question is that how to create a new matrix or vector
from A so that the new vector be sums of every 4 values of
A. Therefore the new vector of B will have a length of 5.

B=[11,14,14,17,10];

How to create B from A?

Thank you very much.
Amir

Subject: Create a new vector from a current vector

From: John D'Errico

Date: 5 Nov, 2007 15:26:40

Message: 2 of 8

"Amir Safari" <amsa36060@yahoo.com> wrote in message <fgnbec$21u
$1@fred.mathworks.com>...
>
> Hi MATLAB users,
>
> Suppose we have a matrix or vector with only one column of
> length 20.
>
> A=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
>
> My question is that how to create a new matrix or vector
> from A so that the new vector be sums of every 4 values of
> A. Therefore the new vector of B will have a length of 5.
>
> B=[11,14,14,17,10];
>
> How to create B from A?
>
> Thank you very much.
> Amir

Easy enough.

A=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];

C = repmat({ones(4,1)},1,5);
A*blkdiag(C{:})

ans =
    11 14 14 17 10

John

Subject: Create a new vector from a current vector

From: nor ki

Date: 5 Nov, 2007 15:42:07

Message: 3 of 8

"Amir Safari" <amsa36060@yahoo.com> wrote in message
<fgnbec$21u$1@fred.mathworks.com>...
>
> Hi MATLAB users,
>
> Suppose we have a matrix or vector with only one column of
> length 20.
>
> A=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
>
> My question is that how to create a new matrix or vector
> from A so that the new vector be sums of every 4 values of
> A. Therefore the new vector of B will have a length of 5.
>
> B=[11,14,14,17,10];
>
> How to create B from A?
>
> Thank you very much.
> Amir


aa = reshape(A, 4,numel(A)/4);
B = sum(aa)

should do

Subject: Create a new vector from a current vector

From: us

Date: 5 Nov, 2007 16:03:15

Message: 4 of 8

Amir Safari:
<SNIP wants to sum subvecs of a larger one

others have shown you the typical way to do it...
one of the more versatile solutions is outlined below

% the data
     a=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
% - define the starting indices of your subvecs
     abeg=[1,6,7,10,18];
% the engine
     aend=[abeg(2:end)-1,numel(a)];
     r=arrayfun(@(b,e) sum(a(b:e)),abeg,aend);
% the result
     [abeg;aend] % the subvecs
     r

us

Subject: Create a new vector from a current vector

From: nor ki

Date: 5 Nov, 2007 16:25:41

Message: 5 of 8

"us " <us@neurol.unizh.ch> wrote in message
<fgnes3$mop$1@fred.mathworks.com>...
> Amir Safari:
> <SNIP wants to sum subvecs of a larger one
>
> others have shown you the typical way to do it...
> one of the more versatile solutions is outlined below
>
> % the data
> a=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
> % - define the starting indices of your subvecs
> abeg=[1,6,7,10,18];
> % the engine
> aend=[abeg(2:end)-1,numel(a)];
> r=arrayfun(@(b,e) sum(a(b:e)),abeg,aend);
> % the result
> [abeg;aend] % the subvecs
> r
>
> us

sorry i don´t get the same results

Subject: Create a new vector from a current vector

From: us

Date: 5 Nov, 2007 17:20:37

Message: 6 of 8

nor ki:
<SNIP found a mistake in <us>'s reply - did he/she?

> sorry i don´t get the same results...

what results? i did not show them...

% here they are
     [abeg;aend]
% 1 6 7 10 18
% 5 6 9 17 20

     r
% 13 4 13 27 9

% now, compare a few results, eg
     sum(a(1:5)) % = 13
     sum(a(10:17)) % = 27
% which seem ok...

note: i said <more versatile>
us

Subject: Create a new vector from a current vector

From: PB

Date: 5 Nov, 2007 20:52:29

Message: 7 of 8

Den Mon, 05 Nov 2007 15:04:45 +0000 skrev Amir Safari:

> Hi MATLAB users,
>
> Suppose we have a matrix or vector with only one column of length 20.
>
> A=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
>
> My question is that how to create a new matrix or vector from A so that
> the new vector be sums of every 4 values of A. Therefore the new vector
> of B will have a length of 5.
>
> B=[11,14,14,17,10];
>
> How to create B from A?
>
> Thank you very much.
> Amir

Another solution

b=filter([1 1 1 1],1,A);
b=b(4:4:end)

/PB

Subject: Create a new vector from a current vector

From: nor ki

Date: 6 Nov, 2007 11:57:11

Message: 8 of 8

"us " <us@neurol.unizh.ch> wrote in message
<fgnjd5$ss1$1@fred.mathworks.com>...
> nor ki:
> <SNIP found a mistake in <us>'s reply - did he/she?
>
> > sorry i don´t get the same results...
>
> what results? i did not show them...
>
> % here they are
> [abeg;aend]
> % 1 6 7 10 18
> % 5 6 9 17 20
>
> r
> % 13 4 13 27 9
>
> % now, compare a few results, eg
> sum(a(1:5)) % = 13
> sum(a(10:17)) % = 27
> % which seem ok...
>
> note: i said <more versatile>
> us

sorry for my stupid remark
i just like to understand your solution

the original desired result was
B=[11,14,14,17,10];

then
     abeg = 1:4:numel(a)
     aend = 4:4:numel(a)
would do it?

nor ki

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sum us 5 Nov, 2007 12:25:07
arrayfun us 5 Nov, 2007 11:05:09
code us 5 Nov, 2007 11:05:09
rssFeed for this Thread

Contact us at files@mathworks.com