How to index and find the resulting matrix?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Pooneh Shah Malekpoor
le 8 Mar 2023
Commenté : Pooneh Shah Malekpoor
le 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 )
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1317860/image.jpeg)
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 commentaires
Réponse acceptée
Dyuman Joshi
le 8 Mar 2023
Modifié(e) : Dyuman Joshi
le 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!