How to index and find the resulting matrix?

2 views (last 30 days)
Hello
There is a mesh where I have the coordinates of the centres of each element as seen in this image (16 elements in total where the distance between centres of neighbouring elements is always 0.5 m either in veritcal or horizontal directions )
If I want to find the correlation matrix, I have to calculate this formula between every two elements of the mesh:
rho(element1, element2) = exp(-sqrt(2*(xi-xj)/tx)^2+(2*(yi-yj)/ty)^2)
where tx=200 and ty=1.5.
I need guidance regarding the indexing, as correlation between elemets 1 and 3 differs from the correlation between elements 1 and 9 . In other words, how to write a for loop to find the rho matrix which contains correlation between different elements?
The problem is relating the label of the element to the i and j coordinates !

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 8 Mar 2023
Edited: Dyuman Joshi on 8 Mar 2023
[x,y]=meshgrid(0.25:0.5:1.75);
%Transposing as linear indexing in MATLAB is column wise
x=x'
x = 4×4
0.2500 0.2500 0.2500 0.2500 0.7500 0.7500 0.7500 0.7500 1.2500 1.2500 1.2500 1.2500 1.7500 1.7500 1.7500 1.7500
y=y'
y = 4×4
0.2500 0.7500 1.2500 1.7500 0.2500 0.7500 1.2500 1.7500 0.2500 0.7500 1.2500 1.7500 0.2500 0.7500 1.2500 1.7500
%Required output is 16x16 matrix
s=size(x).^2;
%Preallocation
out=zeros(s);
tx=200;
ty=1.5;
%Defining formula as a function handle
rho = @(xi,xj,yi,yj) exp(-sqrt(2*(xi-xj)/tx)^2+(2*(yi-yj)/ty)^2);
%Note the order of input
%Double for loop, to go through each pair of elements
for idx=1:s(1)
for jdx=1:s(2)
out(idx,jdx)=rho(x(idx),x(jdx),y(idx),y(jdx));
end
end
disp(out)
1.0000 1.0050 1.0101 1.0151 1.5596 1.5674 1.5753 1.5832 5.9167 5.9464 5.9762 6.0061 54.5982 54.8718 55.1469 55.4233 0.9950 1.0000 1.0050 1.0101 1.5518 1.5596 1.5674 1.5753 5.8872 5.9167 5.9464 5.9762 54.3258 54.5982 54.8718 55.1469 0.9900 0.9950 1.0000 1.0050 1.5441 1.5518 1.5596 1.5674 5.8578 5.8872 5.9167 5.9464 54.0549 54.3258 54.5982 54.8718 0.9851 0.9900 0.9950 1.0000 1.5364 1.5441 1.5518 1.5596 5.8286 5.8578 5.8872 5.9167 53.7853 54.0549 54.3258 54.5982 1.5596 1.5674 1.5753 1.5832 1.0000 1.0050 1.0101 1.0151 1.5596 1.5674 1.5753 1.5832 5.9167 5.9464 5.9762 6.0061 1.5518 1.5596 1.5674 1.5753 0.9950 1.0000 1.0050 1.0101 1.5518 1.5596 1.5674 1.5753 5.8872 5.9167 5.9464 5.9762 1.5441 1.5518 1.5596 1.5674 0.9900 0.9950 1.0000 1.0050 1.5441 1.5518 1.5596 1.5674 5.8578 5.8872 5.9167 5.9464 1.5364 1.5441 1.5518 1.5596 0.9851 0.9900 0.9950 1.0000 1.5364 1.5441 1.5518 1.5596 5.8286 5.8578 5.8872 5.9167 5.9167 5.9464 5.9762 6.0061 1.5596 1.5674 1.5753 1.5832 1.0000 1.0050 1.0101 1.0151 1.5596 1.5674 1.5753 1.5832 5.8872 5.9167 5.9464 5.9762 1.5518 1.5596 1.5674 1.5753 0.9950 1.0000 1.0050 1.0101 1.5518 1.5596 1.5674 1.5753 5.8578 5.8872 5.9167 5.9464 1.5441 1.5518 1.5596 1.5674 0.9900 0.9950 1.0000 1.0050 1.5441 1.5518 1.5596 1.5674 5.8286 5.8578 5.8872 5.9167 1.5364 1.5441 1.5518 1.5596 0.9851 0.9900 0.9950 1.0000 1.5364 1.5441 1.5518 1.5596 54.5982 54.8718 55.1469 55.4233 5.9167 5.9464 5.9762 6.0061 1.5596 1.5674 1.5753 1.5832 1.0000 1.0050 1.0101 1.0151 54.3258 54.5982 54.8718 55.1469 5.8872 5.9167 5.9464 5.9762 1.5518 1.5596 1.5674 1.5753 0.9950 1.0000 1.0050 1.0101 54.0549 54.3258 54.5982 54.8718 5.8578 5.8872 5.9167 5.9464 1.5441 1.5518 1.5596 1.5674 0.9900 0.9950 1.0000 1.0050 53.7853 54.0549 54.3258 54.5982 5.8286 5.8578 5.8872 5.9167 1.5364 1.5441 1.5518 1.5596 0.9851 0.9900 0.9950 1.0000
Here out(X,Y) will correspond to correlation of Xth andYth elements.
  2 Comments
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor on 8 Mar 2023
Thanks for your response. but it doesnot yield correct result:
assume the correlation between first and second element:
then:
rho(1,2)=exp(-sqrt((2*(0.25-0.75))/tx)^2+(2*(0.25-0.25))/ty)^2)=0.995 not 1.5596

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!