How to index and find the resulting matrix?
2 views (last 30 days)
Show older comments
Pooneh Shah Malekpoor
on 8 Mar 2023
Commented: Pooneh Shah Malekpoor
on 8 Mar 2023
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 !
0 Comments
Accepted Answer
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'
y=y'
%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)
Here out(X,Y) will correspond to correlation of Xth andYth elements.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!