How to test a correlation? - matlab

How to test wheater there is a correlation between two parameters? I have n-dimensional parameters a and b in matlab? I would like to find the correlation parametr is there a simply function for it? Thank you
I tried:
C = [a b];
cov(C)
I got
0.0001 0.0029
0.0029 1.0668
And
corrcoef(C)
with the result
1.0000 0.3682
0.3682 1.0000
I sould look at diagonnal members - they are 1, so it is correlation?
Is that right?
1
1.0000 -0.3143
-0.3143 1.0000
2
1.0000 0.4248
0.4248 1.0000
3
1.0000 -0.3290
-0.3290 1.0000
4
1.0000 -0.1397
-0.1397 1.0000
5
1.0000 -0.1506
-0.1506 1.0000
6
1.0000 0.3682
0.3682 1.0000

Related

How to read a .mtl model file into MATLAB and visualise it?

Does anyone know how to read a .mtl 3D model file into MATLAB and visualise it?
The .mtl file looks like following
newmtl body
Ns 51.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 1.0000 1.0000 1.0000
Kd 1.0000 1.0000 1.0000
Ks 0.5400 0.5400 0.5400
Ke 0.0000 0.0000 0.0000
map_Ka body_diff.jpg
map_Kd body_diff.jpg
I tried using MTL_parser.m from https://uk.mathworks.com/matlabcentral/fileexchange/39073-landsat-mss-tm-etm-metadata-mtl-parser?s_tid=srchtitle
Thanks
Maybe you want to refer to this function here: https://au.mathworks.com/matlabcentral/fileexchange/27982-wavefront-obj-toolbox

How to find non zero minimum array in a 2D matrix in matlab

I have a 2D matrix as shown below:
A =
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8193 0.6429 0.4731
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8172 0.6395 0.4696
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8099 0.6285 0.4582
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7938 0.6066 0.4364
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7588 0.5675 0.4006
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8527 0.6739 0.5039 0.3477
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8825 0.7371 0.5803 0.4264 0.2807
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7928 0.6327 0.4838 0.3406 0.2054
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8846 0.7945 0.6560 0.5173 0.3816 0.2469 0.1131
1.0000 1.0000 1.0000 1.0000 1.0000 0.8598 0.7438 0.6373 0.5194 0.3988 0.2785 0.1522 0
1.0000 1.0000 1.0000 0.8710 0.7956 0.6954 0.5935 0.4916 0.3856 0.2799 0.1814 0.0834 0
0.7915 0.7845 0.7581 0.6884 0.6159 0.5326 0.4432 0.3500 0.2514 0.1538 0.0838 0 0
0.5972 0.5882 0.5596 0.5085 0.4471 0.3760 0.2967 0.2137 0.1163 0 0 0 0
0.4208 0.4116 0.3836 0.3389 0.2881 0.2274 0.1540 0.0919 0 0 0 0 0
0.2629 0.2536 0.2244 0.1755 0.1388 0.0915 0 0 0 0 0 0 0
0.1235 0.1155 0.0850 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
I want to remove its zero arrays and then find the minimum value of each column and put those minimum values in a vector.
I just used find(A ~= 0) but it's not working.
How can I do that?
Replace all of the zeroes with NaN, then apply min to each column independently. Setting values to NaN in a matrix / vector allows min to ignore those values, so you'd effectively be ignoring the zeroes. Assuming that your matrix is in A, do this:
Anan = A;
Anan(A == 0) = NaN;
out = min(Anan, [], 1);

MATLAB code for Hamacher sum

The hamacher sum is :
I wrote the following MATLAB code for the above function
function f=hamachersum(x,y)
f = zeros(numel(x),1);
for j=1:numel(x)
if x(j)==1 && y(j)==1
f(j,1)=1;
else
f(j,1)=(x(j)+y(j)-2*(x(j)*y(j)))/(1-(x(j)*y(j)));
end
end
end
Then I want to test t3=hamachersum(t1,t2)
My input values t1, t2 are
t1
t1 =
1.0000
0
1.0000
1.0000
1.0000
1.0000
NaN
0.8167
1.0000
1.0000
1.0000
0.4667
NaN
1.0000
1.0000
1.0000
NaN
NaN
1.0000
1.0000
1.0000
NaN
0.0250
1.0000
t2
t2 =
1.0000
0.5524
1.0000
1.0000
1.0000
1.0000
NaN
0
1.0000
1.0000
1.0000
1.0000
NaN
1.0000
1.0000
1.0000
NaN
NaN
0.6032
1.0000
1.0000
NaN
0.9973
0.7260
The result is
t3 =
1.2000
0.5524
1.0000
1.0000
1.0000
1.0000
NaN
0.8167
2.0000
2.0000
1.0000
1.0000
NaN
0.6667
1.0000
1.0769
NaN
NaN
1.0000
1.0000
1.0000
NaN
0.9973
1.0000
Why do I get values above 1. As this is a fuzzy operator it can't have values above 1.
Is there something wrong in my code?
I am probably answering this against my better judgement.
Caveat: I am not familiar with the Hamacher sum so my approach to answering this is strictly based on the equation in your question.
Is there something wrong in my code?
Your code produces 1.0 when I run it on MATLAB R2014a for inputs of 1.0 and 1.0, I'm presuming that is correct as you have an explicit condition for it. I cant produce the results you are are seeing in your question.
However, I felt compelled to provide a more efficient implementation of the equation
function h = hamachersum(mu_a, mu_b)
h = (mu_a + mu_b - (2 .* mu_a .* mu_b)) ./ (1 - mu_a .* mu_b);
% h(isnan(h)) = 1.0; % Included this line to show you how to remove NaN
end
Note: I've included % h(isnan(h)) = 1.0; to show you how to handle cases when mu_a and mu_b are both 1.0 as you have explicitly handled this in your question (rather poorly might I add).
Comparing floating point numbers is not reliable even in MATLAB and could be part of the reason why you are receiving the results you are. A better way to check what the value of a floating point number is would be to use
if (x - 1.0 > 1e-15)
fprintf(1, "x == 1.0");
else
fprintf(1, "x ~= 1.0");
end
If x is equal to 1.0 down to machine precision this expression will be true otherwise it will be false.

Adding data in intervals in Matlab

Hi I have data in MATLAB like this:
F =
1.0000 1.0000
2.0000 1.0000
3.0000 1.0000
3.1416 9.0000
4.0000 1.0000
5.0000 1.0000
6.0000 1.0000
6.2832 9.0000
7.0000 1.0000
8.0000 1.0000
9.0000 1.0000
9.4248 9.0000
10.0000 1.0000
I am looking for a way to sum the data in specific intervals. Example if I want my sampling interval to be 1, then the end result should be:
F =
1.0000 1.0000
2.0000 1.0000
3.0000 10.0000
4.0000 1.0000
5.0000 1.0000
6.0000 10.0000
7.0000 1.0000
8.0000 1.0000
9.0000 10.0000
10.0000 1.0000
i.e data is accumulated in the second column based on sampling the first row. Is there a function in MATLAB to do this?
Yes by combining histc() and accumarray():
F =[1.0000 1.0000;...
2.0000 1.0000;...
3.0000 1.0000;...
3.1416 9.0000;...
4.0000 1.0000;...
5.0000 1.0000;...
6.0000 1.0000;...
6.2832 9.0000;...
7.0000 1.0000;...
8.0000 1.0000;...
9.0000 1.0000;...
9.4248 9.0000;...
10.0000 1.0000];
range=1:0.5:10;
[~,bin]=histc(F(:,1),range);
result= [range.' accumarray(bin,F(:,2),[])]
If you run the code keep in mind that I changed the sampling interval (range) to 0.5.
This code works for all sampling intervals just define your wanted interval as range.
Yes and that's a job for accumarray:
Use the values in column 1 of F to sum (default behavior of accumarray) the elements in the 2nd column.
For a given interval of size s (Thanks to Luis Mendo for that):
S = accumarray(round(F(:,1)/s),F(:,2),[]); %// or you can use "floor" instead of "round".
S =
1
1
10
1
1
10
1
1
10
1
So constructing the output by concatenation:
NewF = [unique(round(F(:,1)/s)) S]
NewF =
1 1
2 1
3 10
4 1
5 1
6 10
7 1
8 1
9 10
10 1
Yay!!

linear regression with multiple variables in matlab, formula and code do not match

I have the following datasets:
X
X =
1.0000 0.1300 -0.2237
1.0000 -0.5042 -0.2237
1.0000 0.5025 -0.2237
1.0000 -0.7357 -1.5378
1.0000 1.2575 1.0904
1.0000 -0.0197 1.0904
1.0000 -0.5872 -0.2237
1.0000 -0.7219 -0.2237
1.0000 -0.7810 -0.2237
1.0000 -0.6376 -0.2237
1.0000 -0.0764 1.0904
1.0000 -0.0009 -0.2237
1.0000 -0.1393 -0.2237
1.0000 3.1173 2.4045
1.0000 -0.9220 -0.2237
1.0000 0.3766 1.0904
1.0000 -0.8565 -1.5378
1.0000 -0.9622 -0.2237
1.0000 0.7655 1.0904
1.0000 1.2965 1.0904
1.0000 -0.2940 -0.2237
1.0000 -0.1418 -1.5378
1.0000 -0.4992 -0.2237
1.0000 -0.0487 1.0904
1.0000 2.3774 -0.2237
1.0000 -1.1334 -0.2237
1.0000 -0.6829 -0.2237
1.0000 0.6610 -0.2237
1.0000 0.2508 -0.2237
1.0000 0.8007 -0.2237
1.0000 -0.2034 -1.5378
1.0000 -1.2592 -2.8519
1.0000 0.0495 1.0904
1.0000 1.4299 -0.2237
1.0000 -0.2387 1.0904
1.0000 -0.7093 -0.2237
1.0000 -0.9584 -0.2237
1.0000 0.1652 1.0904
1.0000 2.7864 1.0904
1.0000 0.2030 1.0904
1.0000 -0.4237 -1.5378
1.0000 0.2986 -0.2237
1.0000 0.7126 1.0904
1.0000 -1.0075 -0.2237
1.0000 -1.4454 -1.5378
1.0000 -0.1871 1.0904
1.0000 -1.0037 -0.2237
theta
0
0
0
y
y =
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
239999
347000
329999
699900
259900
449900
299900
199900
499998
599000
252900
255000
242900
259900
573900
249900
464500
469000
475000
299900
349900
169900
314900
579900
285900
249900
229900
345000
549000
287000
368500
329900
314000
299000
179900
299900
239500
The X set represents values for multiple variable regression, the first colum stands for X0, second X1; and so on.
The implementation formula is something like:
I have implemented a matlab code which is:
for i=1:size(theta,1)
h=X*theta;
sumE=sum((h-y).*X(:,i));
theta(i)=theta(i)-alpha*(1/m)*sumE;
end
which is inside a for loop going from 1 to a n number of iterations (the value of m is not relevant, it can be set up to 40 for example).
The problem is that even though the code works and the result is the one expected, when I submit it to a online checking program it appears that my results are wrong. The reason is that I should update theta simultaneously.
I have gotten the following Matlab code from Internet:
h = X*theta;
theta = theta - alpha / m * (X'*(h - y));
when I run the Internet solution it gives me almost the same answer as mine, with only a subtle difference in the 6th decimal position. When I submit that answer to the online program it is fully accepted, but I was wondering where the summation goes? in the formula explicitly indicates a summation which is no longer in the Internet solution. Maybe both codes are fine, but I do not know if the Internet author has made some linear algebra trick. Any help?
Thanks
I am not sure if I understood your question, but the formula you copied from internet is X'(h-y). Note that there is a tranposition signal after X! So, this is a matrices product. Your sum (your loop) is replaced by this matrices product.
Their code simultaneously updates theta. Your code iterates through the rows of theta using newer values of theta to regenerate the h which is used in updating the later rows of theta. I'd bet heavily that this is the difference.
For clarity, let's keep track of each iteration of theta in a matrix.
Their code for iteration j is:
h = X*theta(:,j);
theta(:,j+1) = theta(:,j) - alpha / m * (X'*(h - y));
On the other hand, your code would be:
for i=1:size(theta,1)
my_mismatched_theta = [theta(1:i-1, j+1); theta(i:end, j)];
h=X * my_mismatched_theta;
sumE=sum((h-y).*X(:,i));
theta(i,j)=theta(i,j)-alpha*(1/m)*sumE;
end
It doesn't simultaneously update theta in one step. You're using newer versions of theta (i.e. theta(:,j+1) ) to generate h when updating the later rows of theta.
something you should try
Change your code to what I have below and see if you then get the same answer:
h=X*theta; %placed outside of loop so it doesn't get updated by new theta values
for i=1:size(theta,1)
sumE=sum((h-y).*X(:,i));
theta(i)=theta(i)-alpha*(1/m)*sumE;
end
Your algorithm may converge to the same point as theirs in this case, but there's a chance that the sort of casecade updating you're doing creates weirdness in other cases. Who knows.