Work with a data set to compute std

1 view (last 30 days)
Mmotevasseli(alex)
Mmotevasseli(alex) on 6 Mar 2019
Edited: Mmotevasseli(alex) on 8 Mar 2019
I wanna know how should i write a function to generate correlation matrix without using functions like corr,corrcoef,corr2, etc.
i tried different way but didn't work,.
for j=1:7
for k=1:7
mj = mean(X(:,j));
mk = mean(X(:,k));
s(j,k)=1/19 * sum((X(:,j)-mj).*(X(:,k)-mk));
end
end
for l=1:7
for m=1:7
ml=mean(X(:,l));
mm=mean(X(:,m));
standard_dev1=sqrt(1/19 * sum((X(:,l)-ml).^2));
standard_dev2=sqrt(1/19 * sum((X(:,m)-mm).^2));
correlate(l,m)=s(l,m)/standard_dev1*standard_dev2;
end
end
my dataset is a 20*7 matrix
main diameter of correlation must be 1.
I had trouble understanding the formula.
  2 Comments
Jos (10584)
Jos (10584) on 6 Mar 2019
why don't you want to use the right tool for this job?
Steven Lord
Steven Lord on 6 Mar 2019
Usually when someone says "I need to write a function to do X but I can't use any of the functions included in MATLAB to do X or parts of X" it's because they've been told to do so as part of a homework assignment or school project. If that's the case, where are you having difficulty? Are you having trouble determining what you need to write or actually writing it?
If you're stuck determining what you need to write, check if your textbook contains some pseudocode you can use as a guide. Put the pseudocode in a MATLAB program file as comments, then start implementing each comment in turn. If not, check with your professor and/or teaching assistant.
If you're not sure how to write the algorithm you've found show us what you've written so far, show us exactly what happens when you try to run it (copy and paste the full text of any error or warning messages rather than paraphrasing), and describe where you're having difficulty and we may be able to offer some suggestions.

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 6 Mar 2019
Step 1 - Write a function that calculates the correlation between two vectors X and Y with equal lengths, using the formula you can find, e.g., here: https://www.dummies.com/education/math/statistics/how-to-calculate-a-correlation/
function C = MyCorrelationFunction(X,Y)
meanX = mean(X) ;
meanY = mean(Y) ;
sdX = std(X) ;
% etc, you can implement this yourself
C = ...
Step 2 - call this function for each pair of columns in your matrix M
for a=1:size(M,2)
for b = 1:size(M,2)
MyCorrelations(a,b) = MyCorrelationFunction(M(:,a),M(:,b)) ;
end
end
  3 Comments
Jos (10584)
Jos (10584) on 7 Mar 2019
Does your function return a single value?
For instance, what does mycorrelation([1 2 3],[1 2 4]) return?
Jos (10584)
Jos (10584) on 7 Mar 2019
a correlation between two vectors should be a single number.
There must be an error inside the function mycorrelation.m, I think.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!