How to index and find the resulting matrix?
2 ビュー (過去 30 日間)
古いコメントを表示
Pooneh Shah Malekpoor
2023 年 3 月 8 日
コメント済み: Pooneh Shah Malekpoor
2023 年 3 月 8 日
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 件のコメント
採用された回答
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!