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:
storing output of computation into an array

Subject: storing output of computation into an array

From: Pg

Date: 20 Nov, 2011 18:15:14

Message: 1 of 5

hi,

I h've an array A = [1 2 3; 56 7 8; 9 10 11]. Like this my actual A matrix size is 420x151. I want to compute moving average of each row of matrix A and store the output to a new matrix B of size (420-mx151), where m is the sliding window size. In what way I can do so. Plz. let me know.

Subject: storing output of computation into an array

From: Rune Allnor

Date: 20 Nov, 2011 18:48:19

Message: 2 of 5

On 20 Nov, 19:15, "Pg " <poulomi...@gmail.com> wrote:
> hi,
>
> I h've an array A = [1 2 3; 56 7 8; 9 10 11]. Like this my actual A matrix size is 420x151. I want to compute moving average of each row of matrix A and store the output to a new matrix B of size (420-mx151), where m is the sliding window size. In what way I can do so. Plz. let me know.

You are asking the wrong question.

Before you ask *how*, you must first ask *what*
you are going to do and *why*.

I can't see anr yeason why applying an M-length
MA to an N-length sequence would produce a
result of length M*N. Find out what you are
supposed to do first, and the 'how' is trivial.

Rune

Subject: storing output of computation into an array

From: Bruno Luong

Date: 20 Nov, 2011 19:22:14

Message: 3 of 5

"Pg " <poulomizca@gmail.com> wrote in message <jabg3i$sjk$1@newscl01ah.mathworks.com>...
> hi,
>
> I h've an array A = [1 2 3; 56 7 8; 9 10 11]. Like this my actual A matrix size is 420x151. I want to compute moving average of each row of matrix A and store the output to a new matrix B of size (420-mx151), where m is the sliding window size. In what way I can do so. Plz. let me know.

If you are talking about moving average along rows (first dimension), then:

B = conv2(A,ones(m,1),'valid') / m

should do

Bruno

Subject: storing output of computation into an array

From: Roger Stafford

Date: 20 Nov, 2011 19:25:29

Message: 4 of 5

"Pg " <poulomizca@gmail.com> wrote in message <jabg3i$sjk$1@newscl01ah.mathworks.com>...
> ..... I want to compute moving average of each row of matrix A and store the output to a new matrix B of size (420-mx151), where m is the sliding window size. .....
- - - - - - - - - -
  Try this:

 B = [zero(size(A,1),1),cumsum(A,2)];
 B = (B(:,m+1:end)-B(:,1:end-m))/m;

  Note 1: On a 420 by 151 matrix, it is the rows that are of length 151, so it is these rows whose length will be reduced to 151-(m-1) length giving a 420 by 152-m size to B.

  Note 2: The 'cumsum' function should not be used with excessively large numbers of summands since this can produce significantly larger amounts of accumulated round off error than would occur if each moving average sum were calculated separately.

Roger Stafford

Subject: storing output of computation into an array

From: Pg

Date: 21 Nov, 2011 02:38:08

Message: 5 of 5

I have already a program for moving average that I want to implement. Now this program gives output one by one if I take a for loop. Now what I want is to store each result into an empty array. It will be Like :

for i=1:size(A,2) % for each row of matrix A
....my moving avg. program
  ..... output of moving average for each i
end

now I want to store these individual i values in a out put file or an empty matrix. That was my question? In what way i can do it? Sorry I cudn't frame my question properly before.








"Roger Stafford" wrote in message <jabk79$d58$1@newscl01ah.mathworks.com>...
> "Pg " <poulomizca@gmail.com> wrote in message <jabg3i$sjk$1@newscl01ah.mathworks.com>...
> > ..... I want to compute moving average of each row of matrix A and store the output to a new matrix B of size (420-mx151), where m is the sliding window size. .....
> - - - - - - - - - -
> Try this:
>
> B = [zero(size(A,1),1),cumsum(A,2)];
> B = (B(:,m+1:end)-B(:,1:end-m))/m;
>
> Note 1: On a 420 by 151 matrix, it is the rows that are of length 151, so it is these rows whose length will be reduced to 151-(m-1) length giving a 420 by 152-m size to B.
>
> Note 2: The 'cumsum' function should not be used with excessively large numbers of summands since this can produce significantly larger amounts of accumulated round off error than would occur if each moving average sum were calculated separately.
>
> Roger Stafford

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