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:
calling a function

Subject: calling a function

From: Sean Douglas

Date: 27 Jun, 2010 14:52:05

Message: 1 of 5

hello could someone please help me call a function.
i guess there are 2 options for this function and i want to execute a covariance matrix in matlab. here is the function i am attempting to call:

function [CovMat,CorrMat]=COVARIANCE(X,Y)
     % COVARIANCE Computes covariances and correlations
     n=size(X,1);
     if nargin==2
       X=[X Y]; % Concatenate X and Y
     end
     m=mean(X); % Compute the means
     X=X-m(ones(n,1),:); % Subtract the means
     CovMat=X'*X./n; % Compute the covariance
     if nargout==2 % Compute the correlation, if requested
      s=sqrt(diag(CovMat));
      CorrMat=CovMat./(s*s');
     end


the last thing i tried was to call the function with these commands:

(X,Y) =[1 2 3 5 6;
       5 4 10 11 14;
       7 8 15 14 11;
       8 14 5 10 12;
       8 12 22 6 8;
       10 14 15 15 19]
     
[CovMat,CorrMat]=COVARIANCE(X,Y)


please help me, thank you.

Subject: calling a function

From: Faraz Afzal

Date: 27 Jun, 2010 16:10:32

Message: 2 of 5

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07oil$9sq$1@fred.mathworks.com>...
> hello could someone please help me call a function.
> i guess there are 2 options for this function and i want to execute a covariance matrix in matlab. here is the function i am attempting to call:
>
> function [CovMat,CorrMat]=COVARIANCE(X,Y)
> % COVARIANCE Computes covariances and correlations
> n=size(X,1);
> if nargin==2
> X=[X Y]; % Concatenate X and Y
> end
> m=mean(X); % Compute the means
> X=X-m(ones(n,1),:); % Subtract the means
> CovMat=X'*X./n; % Compute the covariance
> if nargout==2 % Compute the correlation, if requested
> s=sqrt(diag(CovMat));
> CorrMat=CovMat./(s*s');
> end
>
>
> the last thing i tried was to call the function with these commands:
>
> (X,Y) =[1 2 3 5 6;
> 5 4 10 11 14;
> 7 8 15 14 11;
> 8 14 5 10 12;
> 8 12 22 6 8;
> 10 14 15 15 19]
>
> [CovMat,CorrMat]=COVARIANCE(X,Y)
>
>
> please help me, thank you.

Thats Badddd Sean

you should study a little bit about Matlab functions as this can be achieved by spending no more than 5 minutes...
Function can bee called using fucntion handle. Save the file with the name "covariance.m".
IN teh command window prepare your argumens as two vectors X and Y whose covariance is to be found..
Then call your function with the name of the file providing the X and Y created as arguments.. e.g.
Creat Vector X,
Create Vector Y
covariance(X,Y)
Then you will have CovMat giving covarinaces and CorrMat giving correlataion between X and Y..

Soon I will be posting a video on youtube to explain functions for newbies so that they need no more guidance abotu it..
Regards,
Muhammad Faraz

Subject: calling a function

From: Sean Douglas

Date: 27 Jun, 2010 16:41:05

Message: 3 of 5

"Faraz Afzal" <farazafzal@gmail.com> wrote in message <i07t5o$2b3$1@fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07oil$9sq$1@fred.mathworks.com>...
> > hello could someone please help me call a function.
> > i guess there are 2 options for this function and i want to execute a covariance matrix in matlab. here is the function i am attempting to call:
> >
> > function [CovMat,CorrMat]=COVARIANCE(X,Y)
> > % COVARIANCE Computes covariances and correlations
> > n=size(X,1);
> > if nargin==2
> > X=[X Y]; % Concatenate X and Y
> > end
> > m=mean(X); % Compute the means
> > X=X-m(ones(n,1),:); % Subtract the means
> > CovMat=X'*X./n; % Compute the covariance
> > if nargout==2 % Compute the correlation, if requested
> > s=sqrt(diag(CovMat));
> > CorrMat=CovMat./(s*s');
> > end
> >
> >
> > the last thing i tried was to call the function with these commands:
> >
> > (X,Y) =[1 2 3 5 6;
> > 5 4 10 11 14;
> > 7 8 15 14 11;
> > 8 14 5 10 12;
> > 8 12 22 6 8;
> > 10 14 15 15 19]
> >
> > [CovMat,CorrMat]=COVARIANCE(X,Y)
> >
> >
> > please help me, thank you.
>
> Thats Badddd Sean
>
> you should study a little bit about Matlab functions as this can be achieved by spending no more than 5 minutes...
> Function can bee called using fucntion handle. Save the file with the name "covariance.m".
> IN teh command window prepare your argumens as two vectors X and Y whose covariance is to be found..
> Then call your function with the name of the file providing the X and Y created as arguments.. e.g.
> Creat Vector X,
> Create Vector Y
> covariance(X,Y)
> Then you will have CovMat giving covarinaces and CorrMat giving correlataion between X and Y..
>
> Soon I will be posting a video on youtube to explain functions for newbies so that they need no more guidance abotu it..
> Regards,
> Muhammad Faraz


thanks for replying..i did create the mfile but was confussed on callling it, i have seen different ways of calling functions and i keep getting them mixed up. I did get outputs from this function, but i think it is giving me the covariance matrix and i am trying to get the correlation matrix which should have 1's down the diagonal and decimals as an out put. How do i get this function to execute the correlation matrix and not the covariance???? sorry for such trivial questions.

X =

     1
     2
     3
     4


Y =

     2 3
     5 5
     7 4
     8 6


ans =

    1.2500 2.5000 1.0000
    2.5000 5.2500 2.0000
    1.0000 2.0000 1.2500

the m file is above in my first post, and i want the to tell this function (mfiile) to execute the correlation matrix and not the covarianc matirx i am getting for an output.

it seems like CorrMat is one of the functions this mfile can do???

Subject: calling a function

From: Faraz Afzal

Date: 27 Jun, 2010 17:11:05

Message: 4 of 5

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07uv1$qdh$1@fred.mathworks.com>...
> "Faraz Afzal" <farazafzal@gmail.com> wrote in message <i07t5o$2b3$1@fred.mathworks.com>...
> > "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07oil$9sq$1@fred.mathworks.com>...
> > > hello could someone please help me call a function.
> > > i guess there are 2 options for this function and i want to execute a covariance matrix in matlab. here is the function i am attempting to call:
> > >
> > > function [CovMat,CorrMat]=COVARIANCE(X,Y)
> > > % COVARIANCE Computes covariances and correlations
> > > n=size(X,1);
> > > if nargin==2
> > > X=[X Y]; % Concatenate X and Y
> > > end
> > > m=mean(X); % Compute the means
> > > X=X-m(ones(n,1),:); % Subtract the means
> > > CovMat=X'*X./n; % Compute the covariance
> > > if nargout==2 % Compute the correlation, if requested
> > > s=sqrt(diag(CovMat));
> > > CorrMat=CovMat./(s*s');
> > > end
> > >
> > >
> > > the last thing i tried was to call the function with these commands:
> > >
> > > (X,Y) =[1 2 3 5 6;
> > > 5 4 10 11 14;
> > > 7 8 15 14 11;
> > > 8 14 5 10 12;
> > > 8 12 22 6 8;
> > > 10 14 15 15 19]
> > >
> > > [CovMat,CorrMat]=COVARIANCE(X,Y)
> > >
> > >
> > > please help me, thank you.
> >
> > Thats Badddd Sean
> >
> > you should study a little bit about Matlab functions as this can be achieved by spending no more than 5 minutes...
> > Function can bee called using fucntion handle. Save the file with the name "covariance.m".
> > IN teh command window prepare your argumens as two vectors X and Y whose covariance is to be found..
> > Then call your function with the name of the file providing the X and Y created as arguments.. e.g.
> > Creat Vector X,
> > Create Vector Y
> > covariance(X,Y)
> > Then you will have CovMat giving covarinaces and CorrMat giving correlataion between X and Y..
> >
> > Soon I will be posting a video on youtube to explain functions for newbies so that they need no more guidance abotu it..
> > Regards,
> > Muhammad Faraz
>
>
> thanks for replying..i did create the mfile but was confussed on callling it, i have seen different ways of calling functions and i keep getting them mixed up. I did get outputs from this function, but i think it is giving me the covariance matrix and i am trying to get the correlation matrix which should have 1's down the diagonal and decimals as an out put. How do i get this function to execute the correlation matrix and not the covariance???? sorry for such trivial questions.
>
> X =
>
> 1
> 2
> 3
> 4
>
>
> Y =
>
> 2 3
> 5 5
> 7 4
> 8 6
>
>
> ans =
>
> 1.2500 2.5000 1.0000
> 2.5000 5.2500 2.0000
> 1.0000 2.0000 1.2500
>
> the m file is above in my first post, and i want the to tell this function (mfiile) to execute the correlation matrix and not the covarianc matirx i am getting for an output.
>
> it seems like CorrMat is one of the functions this mfile can do???

O my Dear Sweeet Sean.

How cute is this.. :o))

You just need to equalize it to whatever variables to get your both covar and corr matrices....
In the command window just type..
Define ur X and Y matrices like u did earlier..
Then type
[covar,corr] = COVARIANCE(X,Y)
and u will see Corr will have ur corr matrix..
If it works fro you forget not to say thanks.

Regards,
Muhammad Faraz

Subject: calling a function

From: Sean Douglas

Date: 27 Jun, 2010 17:58:07

Message: 5 of 5

"Faraz Afzal" <farazafzal@gmail.com> wrote in message <i080n9$jff$1@fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07uv1$qdh$1@fred.mathworks.com>...
> > "Faraz Afzal" <farazafzal@gmail.com> wrote in message <i07t5o$2b3$1@fred.mathworks.com>...
> > > "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <i07oil$9sq$1@fred.mathworks.com>...
> > > > hello could someone please help me call a function.
> > > > i guess there are 2 options for this function and i want to execute a covariance matrix in matlab. here is the function i am attempting to call:
> > > >
> > > > function [CovMat,CorrMat]=COVARIANCE(X,Y)
> > > > % COVARIANCE Computes covariances and correlations
> > > > n=size(X,1);
> > > > if nargin==2
> > > > X=[X Y]; % Concatenate X and Y
> > > > end
> > > > m=mean(X); % Compute the means
> > > > X=X-m(ones(n,1),:); % Subtract the means
> > > > CovMat=X'*X./n; % Compute the covariance
> > > > if nargout==2 % Compute the correlation, if requested
> > > > s=sqrt(diag(CovMat));
> > > > CorrMat=CovMat./(s*s');
> > > > end
> > > >
> > > >
> > > > the last thing i tried was to call the function with these commands:
> > > >
> > > > (X,Y) =[1 2 3 5 6;
> > > > 5 4 10 11 14;
> > > > 7 8 15 14 11;
> > > > 8 14 5 10 12;
> > > > 8 12 22 6 8;
> > > > 10 14 15 15 19]
> > > >
> > > > [CovMat,CorrMat]=COVARIANCE(X,Y)
> > > >
> > > >
> > > > please help me, thank you.
> > >
> > > Thats Badddd Sean
> > >
> > > you should study a little bit about Matlab functions as this can be achieved by spending no more than 5 minutes...
> > > Function can bee called using fucntion handle. Save the file with the name "covariance.m".
> > > IN teh command window prepare your argumens as two vectors X and Y whose covariance is to be found..
> > > Then call your function with the name of the file providing the X and Y created as arguments.. e.g.
> > > Creat Vector X,
> > > Create Vector Y
> > > covariance(X,Y)
> > > Then you will have CovMat giving covarinaces and CorrMat giving correlataion between X and Y..
> > >
> > > Soon I will be posting a video on youtube to explain functions for newbies so that they need no more guidance abotu it..
> > > Regards,
> > > Muhammad Faraz
> >
> >
> > thanks for replying..i did create the mfile but was confussed on callling it, i have seen different ways of calling functions and i keep getting them mixed up. I did get outputs from this function, but i think it is giving me the covariance matrix and i am trying to get the correlation matrix which should have 1's down the diagonal and decimals as an out put. How do i get this function to execute the correlation matrix and not the covariance???? sorry for such trivial questions.
> >
> > X =
> >
> > 1
> > 2
> > 3
> > 4
> >
> >
> > Y =
> >
> > 2 3
> > 5 5
> > 7 4
> > 8 6
> >
> >
> > ans =
> >
> > 1.2500 2.5000 1.0000
> > 2.5000 5.2500 2.0000
> > 1.0000 2.0000 1.2500
> >
> > the m file is above in my first post, and i want the to tell this function (mfiile) to execute the correlation matrix and not the covarianc matirx i am getting for an output.
> >
> > it seems like CorrMat is one of the functions this mfile can do???
>
> O my Dear Sweeet Sean.
>
> How cute is this.. :o))
>
> You just need to equalize it to whatever variables to get your both covar and corr matrices....
> In the command window just type..
> Define ur X and Y matrices like u did earlier..
> Then type
> [covar,corr] = COVARIANCE(X,Y)
> and u will see Corr will have ur corr matrix..
> If it works fro you forget not to say thanks.
>
> Regards,
> Muhammad Faraz

Muhammad my friend, thank you...

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