How to index and find the resulting matrix?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dyuman Joshi
2023-3-8
编辑:Dyuman Joshi
2023-3-8
[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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!