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:
Very quick question about if then statements and standard deviation?

Subject: Very quick question about if then statements and standard deviation?

From: Laurentiu Galan

Date: 2 Dec, 2010 02:13:04

Message: 1 of 6

I have a matrix matrix with a bunch of returns:

How do I create a second matrix, that will calculate the standard deviations for
a trailing numbers in groups of three? i.e. if the first two numbers will be
0 and the nth number will be the standard deviation of the corresponding
number in matrix 1 and the preceding 2 numbers.

I have the matrix:
matrix: matrix2
-.0001 0
0.003 0
0.012 stdev of (-0.001, 0.003, 0.012)
-.002 I want matrix2: stdev of (0.003, 0.012, -0.002
-.2 stdev of (0.012, -.002, -.2)
. .
. .
. .
0.02
-.07
0.04 stdev of (0.02, -0.07, 0.04)

I know it's a very trivial if then statement, but i am having some difficulty with it.

Thank you so much Matlab Forums community!

Subject: Very quick question about if then statements and standard deviation?

From: Laurentiu Galan

Date: 2 Dec, 2010 02:18:04

Message: 2 of 6

I apologize for the way the format came out, i thought it would be spaced...

Subject: Very quick question about if then statements and standard deviation?

From: Roger Stafford

Date: 2 Dec, 2010 03:15:25

Message: 3 of 6

"Laurentiu Galan" <larryg003@yahoo.com> wrote in message <id6vbg$pjp$1@fred.mathworks.com>...
> I have a matrix matrix with a bunch of returns:
> How do I create a second matrix, that will calculate the standard deviations for
> a trailing numbers in groups of three? i.e. if the first two numbers will be
> 0 and the nth number will be the standard deviation of the corresponding
> number in matrix 1 and the preceding 2 numbers.
> .......
- - - - - - - -
  Assuming M1 and M2 are column vectors,

 M2 = zeros(size(M1));
 M2(3:end) = std([M1(1:end-2),M1(2:end-1),M1(3:end)],0,2);

Roger Stafford

Subject: Very quick question about if then statements and standard deviation?

From: Laurentiu Galan

Date: 2 Dec, 2010 03:33:04

Message: 4 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <id730d$jff$1@fred.mathworks.com>...
> "Laurentiu Galan" <larryg003@yahoo.com> wrote in message <id6vbg$pjp$1@fred.mathworks.com>...
> > I have a matrix matrix with a bunch of returns:
> > How do I create a second matrix, that will calculate the standard deviations for
> > a trailing numbers in groups of three? i.e. if the first two numbers will be
> > 0 and the nth number will be the standard deviation of the corresponding
> > number in matrix 1 and the preceding 2 numbers.
> > .......
> - - - - - - - -
> Assuming M1 and M2 are column vectors,
>
> M2 = zeros(size(M1));
> M2(3:end) = std([M1(1:end-2),M1(2:end-1),M1(3:end)],0,2);
>
> Roger Stafford

THANK YOU SO MUCH ROGER!!!

I suppose you wouldn't know by any chance how to loop with groups of k? Not just 3 that is?

Subject: Very quick question about if then statements and standard deviation?

From: Roger Stafford

Date: 2 Dec, 2010 04:05:21

Message: 5 of 6

"Laurentiu Galan" <larryg003@yahoo.com> wrote in message <id741g$pd5$1@fred.mathworks.com>...
> ......
> I suppose you wouldn't know by any chance how to loop with groups of k? Not just 3 that is?
- - - - - - - - -
  Provided you don't run into memory problems, do this:

 M2 = zeros(size(M1));
 M2(k:end) = std(hankel(M1(1:end-k+1),M1(end-k+1:end)),0,2);

Otherwise you'll have to use a for-loop to take the standard deviations one at a time.

Roger Stafford

Subject: Very quick question about if then statements and standard deviation?

From: Laurentiu Galan

Date: 2 Dec, 2010 04:29:04

Message: 6 of 6

IT WORKS!!!
THANK YOU SO MUCH!!!!!!

i tried to do it on my own and got something like this (resulted in an error)
*This would have been the first loop i created, had this crap worked...

%sample matrix
a=[1;2;3;4;5;6;7;8;9;10;1;2;3;4;5;6]
b=size(a)
M2 = zeros(size(a));
for i=1:13
    for j=0:2
    if i>2
    M2(i,1)=std(a(i-j,1))
    else M2(i,1)=0
    end
    end
end

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